List deduplication Java stream implementation based on a field attribute in the object

Insert image description here

List deduplication Java stream implementation based on a certain field attribute in the object?

In Java's Stream, you can use the distinct method to achieve deduplication based on a field attribute in an object. To implement this feature, you need to override the hashCode and equals methods of the object to ensure that objects with the same field attributes are considered equal. Here is a sample code:

List<YourObject> list = // 原始对象列表
List<YourObject> distinctList = list.stream()
    .distinct()
    .collect(Collectors.toList());

In the above code, YourObject is your custom object type, and you need to rewrite the hashCode and equals methods according to a certain field property of the object.

Note that if your object type (YourObject) has mutable field properties and you want to deduplicate based on the latest value of the field property, then you may need to sort the list first and then deduplicate.

If an object has many existing attributes, how to remove duplicates based on a certain attribute in the object. Implemented using Java streams?

If an object has many attributes, you can use the Java stream (Stream) and collect methods combined with a custom collector (Collector) to deduplicate according to a certain attribute in the object. Here is a sample code:

List<YourObject> list = // 原始对象列表
List<YourObject> distinctList = list.stream()
    .collect(Collectors.collectingAndThen(
        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(YourObject::getProperty))),
        ArrayList::new)
    );

In the above code, you need to replace YourObject with your actual object type, and use getProperty to compare the uniqueness of the object based on the property you want.

In this example, we use the Collectors.toCollection method and provide a TreeSet as a container to implement deduplication. TreeSet will sort and uniquely determine objects based on the comparator we provide. Finally, we use ArrayList to re-collect the deduplicated object list.

Please note that this method assumes that your object type has correctly implemented the hashCode and equals methods to ensure object uniqueness and correct comparison. If the object type does not implement these methods correctly, you may need to implement your own Comparator (Comparator) to define the uniqueness of the object and the comparison method.

Get a certain attribute value in the object from the list and then form a new list using Java stream?

You can use Java Stream to obtain a certain attribute value in the object and form a new list after deduplication. Here is a sample code:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class MyClass {
    
    
    private int id;
    private String name;

    public MyClass(int id, String name) {
    
    
        this.id = id;
        this.name = name;
    }

    public int getId() {
    
    
        return id;
    }

    public String getName() {
    
    
        return name;
    }
}

public class Main {
    
    
    public static void main(String[] args) {
    
    
        List<MyClass> myList = new ArrayList<>();
        myList.add(new MyClass(1, "A"));
        myList.add(new MyClass(2, "B"));
        myList.add(new MyClass(2, "B")); // 添加重复对象
        myList.add(new MyClass(3, "C"));

        List<String> distinctNames = myList.stream()
                .map(MyClass::getName) // 获取对象的name属性值
                .distinct() // 去重
                .collect(Collectors.toList()); // 转换为list

        System.out.println(distinctNames);
    }
}

In the above example code, a MyClass class is defined, which has two attributes: id and name. Use the map operation of the Java stream to map the MyClass object to the name attribute value, then use the distinct operation to remove duplicate attribute values, and finally use the collect operation to convert the stream into a new List.

The output result is: [A, B, C], where duplicate attribute values ​​are removed, and a new attribute value list after deduplication is obtained.

Guess you like

Origin blog.csdn.net/weixin_50503886/article/details/132593326