【java】List<Map<String, Object>> 根据某个属性去重

时间:2025-03-20 08:01:24

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

to do: List<Map<String, Object>> 根据Map对象里面某个属性值去重
方法:使用java8 stream

代码示例:

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

    利用Set集合的元素不重复的特点,构造一个带有构造函数的TreeSet,使用Comparator定义元素的排序顺序:

  • (TreeSet::new)

    返回一个Collector,将输入的元素按照处理的顺序放到新的Collection里面

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