List<Map<String, Object>> map根据某个属性去重

dataList = dataList.stream().collect(Collectors.collectingAndThen(
				Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(p -> (String) p.get("code")))),
				ArrayList::new));

原理:

new TreeSet<>(Comparator.comparing(p -> (String) p.get("code")))
利用Set集合的元素不重复的特点,构造一个带有构造函数的TreeSet,使用Comparator定义元素的排序顺序:

Collectors.toCollection(TreeSet::new)
返回一个Collector,将输入的元素按照处理的顺序放到新的Collection里面

Collectors.collectingAndThen()

调整Collector,执行另外的转换操作

https://blog.csdn.net/MD_MushRoom/article/details/130752667

map 深拷贝
Map<String,Object> mpFor = new HashMap<>(mpKw);
posted @ 2023-08-15 10:41  寒冷的雨呢  阅读(62)  评论(0编辑  收藏  举报