针对List集合去除重复项,使用set集合存入数据,转成List
//定义一个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;
}
}