Detailed usage of distinct deduplication in Stream

Preface

The distinct method can perform deduplication operations in the collection in the Stream stream, but it must be specifically defined according to the data type in the collection. Simple data types and custom data types operate differently.

simple data type

Here we take Lista collection as an example, and the data type in the collection is Integer. For simple data types, you can directly call the distinct method in Stream to perform deduplication. Each value will be compared. If the two values ​​​​are the same, they will be considered duplicates and the duplication will be deduplicated.

List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,4,5));
list.stream()
        .distinct()
        .forEach(a -> System.out.println(a));

result:

1
2
3
4
5

Other simple data types are similar and will not be demonstrated here.

custom data type

If the data type in the collection is a custom object, when distinct is deduplicated, it will be judged based on the equals method in the custom object, so we have to rewrite the equals method in the object, custom The object Author is as follows:

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode//用于后期的去重使用(相当于重写了equals方法,自定义Author对象查重比较),此处自定义的比较是:全部元素相同即对象相同
public class Author {
    
    
    //id
    private Long id;
    //姓名
    private String name;
    //年龄
    private Integer age;
    //简介
    private String intro;
    //作品
    private List<Book> books;

}

The annotations we use lombokare @EqualsAndHashCodeequivalent to overriding the equals method, which defaults to: if all elements in two objects are the same, they will be considered duplicates and the duplicates will be removed.

If we don’t want to judge that all elements are the same before they are considered duplicates, we can override the equals method ourselves. In IDEA, alt + enterselect the button in the picture:
Insert image description here
go all the way to next. In the picture below, select the object you want to judge. Duplicate same fields:
Insert image description here

At this time, only when the four fields of the two objects are the same, the two objects will be considered to be the same. The finally rewritten equals method (there will be an additional hashCode method, this method cannot be removed ) is as shown in the figure:
Insert image description here

Then we can use distinct to deduplicate objects.

Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
Author author2 = new Author(2L, "亚拉索", 15, "狂风也追逐不上他的思考速度", null);
Author author3 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null);
Author author4 = new Author(3L, "易", 15, "是这个世界在限制他的思维", null);

List<Author> authors = getAuthors();
authors.stream()
        .distinct()
        .forEach(author -> System.out.println(author.getName()));

result:

蒙多
亚拉索
易

Guess you like

Origin blog.csdn.net/qq_47188967/article/details/131975702