//定義一個HashSet集合
HashSet<TransfersScheme> nonNewSet = new HashSet<TransfersScheme>();
//遍歷數據庫取出的數據
for (TransferScheme non : nonList) {
//將數據依次存入set集合中
nonNewSet.add(non);
}
//再將定義一個 LIST集合
if (nonNewSet.size() > 0) {
List<Object> list = new ArrayList<Object>();
//將Set集合存入List中。完成 Set集合 轉換 List集合的操作
list.addAll(nonNewSet);
tv.setListLines(list);
tvList.add(tv);
}
//針對去重 需要 對 Class文件 重寫 hashCode和equals方法
//如下Class文件
public class TransfersScheme {
/**
* 線路ID
*/
private String lineId;
/**
* 線路名稱
*/
private String lineName;
private Integer lineType;
/**
* 上車站點
*/
private String upStation;
/**
* 上車站點編號
*/
private String upStationId;
/**
* 下車站點
*/
private String downStation;
/**
* 下車站點編號
*/
private String downStationId;
public String getLineId() {
return lineId;
}
public void setLineId(String lineId) {
this.lineId = lineId;
}
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public String getUpStation() {
return upStation;
}
public void setUpStation(String upStation) {
this.upStation = upStation;
}
public String getDownStation() {
return downStation;
}
public void setDownStation(String downStation) {
this.downStation = downStation;
}
public String getUpStationId() {
return upStationId;
}
public void setUpStationId(String upStationId) {
this.upStationId = upStationId;
}
public String getDownStationId() {
return downStationId;
}
public void setDownStationId(String downStationId) {
this.downStationId = downStationId;
}
public Integer getLineType() {
return lineType;
}
public void setLineType(Integer lineType) {
this.lineType = lineType;
}
//重寫hashCode 獲取String類型屬性的hashCode,與 整型屬性 相乘。
//得到屬性的所有hashCode,JAVA機制進行判斷 對象是否相同,hashCode是否結構一致
@Override
public int hashCode() {
return lineType * upStation.hashCode() * downStation.hashCode() * lineName.hashCode() * upStationId.hashCode() * downStationId.hashCode() * lineId.hashCode();
}
//equals判斷屬性是否一致。
//TransfersScheme ts = (TransfersScheme) obj;
//將THIS對象和ts對象 依次判斷屬性是否相同
@Override
public boolean equals(Object obj) {
TransfersScheme ts = (TransfersScheme) obj;
if (lineType == ts.lineType && lineName.equals(ts.lineName) && upStation.equals(ts.upStation) &&
downStation.equals(ts.downStation) && upStationId.equals(ts.upStationId) && downStationId.equals(ts.downStationId) && lineId.equals(ts.lineId)) {
return true;
}
return false;
}
}