How to deduplicate according to a certain attribute in the object in the list object using Java8 stream implementation

Insert image description here

How to use Java8 stream to deduplicate according to a certain attribute in the object in the list object?

In Java 8 stream operations, you can use the distinct method to deduplicate an object stream, but by default it will use the object's equals() method to determine duplication. If you want to deduplicate according to a certain attribute of the object, you can use the distinct method combined with the map method to achieve.

The following is a sample code. Suppose you have a List object list, which contains multiple objects. Each object has a property property. You want to remove duplicates based on the property property:

List<Object> distinctList = list.stream()
                               .map(obj -> obj.getProperty())
                               .distinct()
                               .collect(Collectors.toList());

In the above example, we first use the map method to map the object into its property attribute, then use the distinct method to deduplicate the property, and finally collect the results into a new List object through the collect method.

Note that the property attribute here needs to correctly implement the equals() and hashCode() methods to ensure accurate deduplication.

If the list object is an entity class, how to deduplicate according to a certain attribute in the entity class?

If you have a list containing entity class objects and want to remove duplicates based on a certain attribute of the entity class, you can use Java 8's stream operations combined with lambda expressions to achieve this.

Suppose you have an entity class Entity, which has an attribute property, and you want to deduplicate according to the property attribute. You can follow these steps:

In the entity class Entity, ensure that the equals() and hashCode() methods are implemented correctly;
use stream operations to convert the list into a stream;
use the distinct method and a lambda expression consisting of an attribute extractor (anonymous function) to perform demultiplexing based on the attributes. Heavy;
use the collect method to collect the stream back into a list.
Here is a sample code:

List<Entity> distinctList = list.stream()
                                .distinct()
                                .collect(Collectors.toList());

In the example, we use the distinct method to deduplicate the stream, and judge whether it is duplicate according to the equals() and hashCode() methods in the entity class. Finally, the results are collected as a list using the collect method.

If you want to remove duplicates based on a certain attribute of the entity class (such as property), you can use the following code:

List<Entity> distinctList = list.stream()
                                .distinct()
                                .collect(Collectors.collectingAndThen(
                                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Entity::getProperty))),
                                    ArrayList::new));

In this example, we used the Collectors.collectingAndThen method to combine two collectors. We first use Collectors.toCollection to create a TreeSet to perform deduplication based on property attributes, and then use the ArrayList constructor to create a new ArrayList object to save the results.

Please note that the Entity class here needs to correctly implement the equals() and hashCode() methods to ensure accurate deduplication.

Guess you like

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