场景
需要对比整行数据进行去重,数据存储结构为嵌套list
姓名 | 身高 |
---|---|
小王 | 170 |
小王 | 180 |
小王8 | 190 |
小王8 | 190 |
小王8 | 180 |
方案
使用 collectingAndThen 函数,先将每行的去重依据字段串成一个字符串,使用 set 集合进行去重,然后映射为去重前的行数据即可
List<List<String>> lists =
Arrays.asList(
Arrays.asList("小王", "170"),
Arrays.asList("小王", "180"),
Arrays.asList("小王8", "190"),
Arrays.asList("小王8", "190"),
Arrays.asList("小王8", "180"));
List<List<String>> collect =
lists.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(o -> String.join("", o)))),
ArrayList::new));
collect.forEach(System.out::println);