package .warning.util;
import java.util.*;
/**
* TODO:根據map裏面兩個字段去重
* @author
* @version 1.0
* @description TODO
* @date 2019/10/18 18:08
**/
public class ListMapUtil {
public static List<Map<String, String>> toDistinctList(List<Map<String, String>> list) {
//新建set集合,利用set集合元素不能重複特點,找出重複數據
Set<DispatchCommandsPo> keysSet = new HashSet<DispatchCommandsPo>();
//定義兩個變量
String equipmentNo = "";
String depotName = "";
Iterator<Map<String, String>> it=list.iterator();
while(it.hasNext()) {
Map<String, String> map=it.next();
//變量用來保存兩個key所對應的值
equipmentNo = (String) map.get("equipmentNo");
depotName = (String) map.get("depotName");
//新建一個對象,並重寫equals和hashcode方法
DispatchCommandsPo dispatchCommandsPo = new DispatchCommandsPo();
dispatchCommandsPo.setEquipmentNo(equipmentNo);
dispatchCommandsPo.setDepotName(depotName);
int beforeSize = keysSet.size();
keysSet.add(dispatchCommandsPo);
int afterSize = keysSet.size();
//判斷當前對象是否保存進set集合中(若未保存說明set集合已存在該數據,刪除該條map數據)
if(afterSize != (beforeSize + 1)) {
it.remove();
}
}
return list;
}
static class DispatchCommandsPo {
private String equipmentNo;
private String depotName;
public String getEquipmentNo() {
return equipmentNo;
}
public void setEquipmentNo(String equipmentNo) {
this.equipmentNo = equipmentNo;
}
public String getDepotName() {
return depotName;
}
public void setDepotName(String depotName) {
this.depotName = depotName;
}
//重寫hashcode方法
@Override
public int hashCode() {
int result = 17;
result = 31 * result + (equipmentNo == null ? 0 : equipmentNo.hashCode());
result = 31 * result + (depotName == null ? 0 : depotName.hashCode());
return result;
}
//重寫equals方法
@Override
public boolean equals(Object obj) {
if (obj instanceof DispatchCommandsPo) {
DispatchCommandsPo dispatchCommandsPo = (DispatchCommandsPo) obj;
if (equipmentNo.equalsIgnoreCase(dispatchCommandsPo.getEquipmentNo().trim()) &&
depotName.equalsIgnoreCase(dispatchCommandsPo.getDepotName().trim())) {
return true;
}
}
return false;
}
}
public static List<Map<String, String>> toDistinctList2(List<Map<String, String>> list) {
//新建set集合,利用set集合元素不能重複特點,找出重複數據
Set<DispatchCommandsPo2> keysSet = new HashSet<DispatchCommandsPo2>();
//定義兩個變量
String fenceName = "";
Iterator<Map<String, String>> it=list.iterator();
while(it.hasNext()) {
Map<String, String> map=it.next();
//變量用來保存兩個key所對應的值
fenceName = (String) map.get("fenceName");
//新建一個對象,並重寫equals和hashcode方法
DispatchCommandsPo2 dispatchCommandsPo2 = new DispatchCommandsPo2();
dispatchCommandsPo2.setFenceName(fenceName);
int beforeSize = keysSet.size();
keysSet.add(dispatchCommandsPo2);
int afterSize = keysSet.size();
//判斷當前對象是否保存進set集合中(若未保存說明set集合已存在該數據,刪除該條map數據)
if(afterSize != (beforeSize + 1)) {
it.remove();
}
}
return list;
}
static class DispatchCommandsPo2 {
private String fenceName;
public String getFenceName() {
return fenceName;
}
public void setFenceName(String fenceName) {
this.fenceName = fenceName;
}
//重寫hashcode方法
@Override
public int hashCode() {
int result = 17;
result = 31 * result + (fenceName == null ? 0 : fenceName.hashCode());
return result;
}
//重寫equals方法
@Override
public boolean equals(Object obj) {
if (obj instanceof DispatchCommandsPo2) {
DispatchCommandsPo2 dispatchCommandsPo2 = (DispatchCommandsPo2) obj;
if (fenceName.equalsIgnoreCase(dispatchCommandsPo2.getFenceName().trim())) {
return true;
}
}
return false;
}
}
}