Java8去重案例4则

@Test
public void streamTest8() {

List<Person> personList = new ArrayList<Person>(); // 创建一个 Person 类型的列表

// 添加多个 Person 对象到列表中,可能包含重复数据
personList.add(new Person("鸣人", 8900, 18, "1", "螺旋丸"));
personList.add(new Person("鸣人", 8900, 18, "1", "螺旋丸"));
personList.add(new Person("小樱", 7800, 17, "2", "治疗术"));
personList.add(new Person("小樱", 7800, 17, "2", "治疗术"));
personList.add(new Person("大蛇丸", 9500, 31,"1","八岐大蛇"));
personList.add(new Person("佐助", 8900 ,18,"2","须佐能乎"));
personList.add(new Person ("纲手" ,7900 ,29 ,"2 ","百豪之术"));

// 根据姓名去重,基于 TreeSet 的比较规则(按姓名)
List<Person> uniqueByName = personList.stream().collect(

   Collectors.collectingAndThen(
       Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))),
       ArrayList::new));

System.out.println("去重后的列表(仅根据姓名):" + uniqueByName);

// 根据工资和年龄联合去重,基于 TreeSet 的比较规则(按工资和年龄拼接)
List<Person> uniqueBySalaryAndAge = personList.stream().collect(

   Collectors.collectingAndThen(
       Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(p -> p.getSalary() + ";" + p.getAge()))),
       ArrayList::new));

System.out.println("去重后的列表(根据工资和年龄):" + uniqueBySalaryAndAge);
}


// 获取列表中重复的元素
public static <E> List<E> getDuplicateElements(List<E> list) {

// 将列表转换成 Stream
return list.stream() 
        // 将元素映射为出现次数构建 Map,键为元素,值为出现次数
        .collect(Collectors.toMap(e -> e, e -> 1, (a, b) -> a + b)) 
        // 对所有 entry 构建 Stream
        .entrySet().stream() 
        // 过滤出出现次数大于 1 的 entry(即重复元素)
        .filter(entry -> entry.getValue() > 1) 
        // 提取这些 entry 中的键(重复元素)并构建对应的 Stream
        .map(entry -> entry.getKey()) 
        // 将重复元素转化为 List 返回结果
        .collect(Collectors.toList());  

}

public static void main(String[] args) throws Exception {

List<String> list = Arrays.asList("a", "b", "c", "d", "a", "a", "d", "d","c","b","e","f");
// 调用方法获取列表中重复的元素
List<String> duplicateElements = getDuplicateElements(list); 
// 打印输出包含在列表中的重复元素信息
System.out.println("list 中重复的元素:" + duplicateElements); 

}


两个集合去重
List<GenTable> list1 = ...; // 一个列表包含在另一个中
List<GenTable> list2 = ...; // 第二个列表

List<GenTable> resultList = Stream.concat(list1.stream(), list2.stream())
        .distinct()
        .collect(Collectors.toList());

首先使用Stream.concat()方法将两个列表合并成一个流,然后使用distinct()方法去除其中的重复元素,最后通过collect(Collectors.toList())将结果收集到一个新的列表中。这种写法更加简洁高效,并且应该可以在大多数情况下满足需求。


List<MyObject> distinctList = myList.stream()
                                    .distinct()
                                    .collect(Collectors.toList());

在这个例子中,先创建了一个列表myList,并填充了一些重复的 MyObject对象。使用stream()方法创建一个流,然后使用distinct()方法在流中去除重复元素。最后,使用collect()方法将去重后的结果收集回一个新的List中。

请注意,这里假设MyObject类正确地实现了equals() 方法和hashCode()方法。这对于去除重复元素是至关重要的,因为Stream API使用equals()方法来比较对象。

如果需要根据某个属性来区分元素并去除重复元素,可以使用distinct()方法的重载版本,并提供一个由该属性生成的函数:

List<MyObject> distinctList = myList.stream()
                                    .distinct(myObject -> myObject.getProperty())
                                    .collect(Collectors.toList());

扫描二维码,在手机上阅读!

发表评论

电子邮件地址不会被公开。 必填项已用*标注