Stream多字段去重
List<Person> uniquePeople = people.stream()
.collect(Collectors.toMap(
person -> StrUtil.join("#", person.getName(), person.getAge(), person.getSex()),
Function.identity(), // 使用Person对象本身作为值
(existing, replacement) -> existing, // 如果有冲突,保留现有的Person对象
LinkedHashMap::new // 使用LinkedHashMap来保留插入顺序(如果需要,否则不要此参数)
))
.values().stream().collect(Collectors.toList());
使用Hutool的StrUtil.join()可避免字符串拼接时出现NPE