Map列表,对其中多个列进行分组,判断重复的实现
- 指定去重的列
- 按照指定的列分组
public static List uniqueList(List<Map<String, String>> rows) {
List test = rows.stream().collect(Collectors.toMap(e -> {
return e.get("column1") + "_" + e.get("column2");
}, e-> {
List result = new ArrayList();
result.add(e);
return result;
}, (a, b) -> {
a.addAll(b);
return a;
})).entrySet().stream().filter(entry -> entry.getValue().size() > 1)
.collect(ArrayList::new, (list, entry)->{
list.addAll(entry.getValue());
},(list1, list2) ->{
list1.addAll(list2);
});
return test;
}
获取重复的key
public static Set getMultiKeys(List<Map<String, String>> rows) {
Set test = rows.stream().collect(Collectors.toMap(e -> {
return e.get("column1") + "_" + e.get("column2");
}, e-> {
List result = new ArrayList();
result.add(e);
return result;
}, (a, b) -> {
a.addAll(b);
return a;
})).entrySet().stream().filter(entry -> entry.getValue().size() > 1)
.collect(HashSet::new, (list, entry)->{
list.add(entry.getKey());
},(list1, list2) ->{
list1.addAll(list2);
});
return test;
}
网友评论