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,执行另外的转换操作