Since the Stream function was implemented in Java, it has become possible to perform deduplication (distinct) from List. However, as described in the article [Java8] Stream to delete duplicates (& duplicate check), the standard function allows duplicate deletion only with the List value. If List is an object, duplicate deletion cannot be performed with the specified key in the object. So, this time I will write how to realize duplicate deletion with the specified key in the object.
As described in the article How to perform Stream.distinct with field properties etc., duplicate deletion is realized by combining filter and HashSet without using distinct. How to do it is introduced. Also, in the article Java 8 Distinct by property, how to use groupingBy, how to wrap classes, and libraries are described. How to use is introduced.
There are various methods, and I'm wondering which one is fine, but this time I will introduce the method using the
<Added on 2020/11/13> In the article How to use Stream distinct in Java8 introduced in @ Rui_K's comment, equals is used in the internal processing of distinct of standard Stream. It seems to be used. So, if you implement the logic of the same judgment in the equals method, it seems that the standard method can also handle it.
What is RxJava? As introduced in the article RxJava-Overview, a library developed to realize reactive programming. is. The distinct method is provided in the Observable class provided by RxJava, and this method allows you to specify the key to be duplicated. Please refer to this Documentation. The sample code is as follows. Here, duplicate deletion is performed by the value of email of User class.
List<User> distinctUserRecords = Observable.fromStream(userRecords.stream()) .distinct(u -> u.getEmail()).toList().blockingGet();