「List列表根据对象的属性进行去重操作」
-
去除List中重复的String -
List中对象的去重 -
List中根据对象的属性去重

「一、去除List中重复的String」
方法1:
/**
* 去除重复的List 方法一
*
* @param sourceList 源List
* @return
*/
public static List<String> removeRepeatList(List<String> sourceList) {
if (CollUtil.isEmpty(sourceList)) {
return Collections.emptyList();
}
Set<String> targetSet = new LinkedHashSet<>();
targetSet.addAll(sourceList);
sourceList.clear();
sourceList.addAll(targetSet);
return sourceList;
}
方法2:
/**
* 去除重复的List 方法二
*
* @param sourceList 源List
* @return
*/
public static List<String> removeRepeatListMethod2(List<String> sourceList) {
if (CollUtil.isEmpty(sourceList)) {
return Collections.emptyList();
}
return sourceList.stream().distinct().collect(Collectors.toList());
}
「二、List中对象去重」
新建Student类
/**
* @author 公众号: SimpleMemory
* @version 1.0.0
* @ClassName Student.java
* @Description TODO
* @createTime 2022年04月16日 12:17:00
*/
@Data
@EqualsAndHashCode
@AllArgsConstructor
public class Student {
private String userId;
private String name;
}
主要实现
/**
* List中对象去重
*
* @param studentList
* @return
*/
public static List<Student> removeRepeatStudentList(List<Student> studentList) {
if (CollUtil.isEmpty(studentList)) {
return Collections.emptyList();
}
List<Student> targetStudentList = new ArrayList<>();
studentList.stream().forEach(item -> {
if (!targetStudentList.contains(item)) {
targetStudentList.add(item);
}
}
);
return studentList;
}
「三、List中根据对象的属性进行去重」
方法1:
/**
* 根据Student userId去重
*
* @param studentList
* @return
*/
public static List<Student> removeRepeatStudentListByUserId(List<Student> studentList) {
if (CollUtil.isEmpty(studentList)) {
return Collections.emptyList();
}
Set<Student> studentSet = new TreeSet<>((o1, o2) -> o1.getUserId().compareTo(o2.getUserId()));
studentSet.addAll(studentList);
return new ArrayList<>(studentSet);
}
方法2:
/**
* 根据Student userId去重 方法二:Java 8写法
*
* @param studentList
* @return
*/
public static List<Student> removeRepeatStudentListByUserIdMethod2(List<Student> studentList) {
if (CollUtil.isEmpty(studentList)) {
return Collections.emptyList();
}
List<Student> targetStudentList = studentList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparing(Student::getUserId))), ArrayList::new)
);
return targetStudentList;
}
方法3:
/**
* 根据Student userId去重 方法三
*
* @param studentList
* @return
*/
public static List<Student> removeRepeatStudentListByUserIdMethod3(List<Student> studentList) {
if (CollUtil.isEmpty(studentList)) {
return Collections.emptyList();
}
return studentList.stream().filter(distinctByKey(p -> p.getUserId())).collect(Collectors.toList());
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

原文始发于微信公众号(SimpleMemory):List列表根据对象的属性进行去重操作
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137918.html