[Java] I want to perform distinct with the key in the object

Overview

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.

Correspondence candidate

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 RxJava library.

<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.

Duplicate removal using RxJava

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.

sample.java


List<User> distinctUserRecords = Observable.fromStream(userRecords.stream())
    .distinct(u -> u.getEmail()).toList().blockingGet();

Recommended Posts

[Java] I want to perform distinct with the key in the object
I want to distinct the duplicated data with has_many through
I want to simplify the conditional if-else statement in Java
I want to return an object in CSV format with multi-line header & filter in Java
I want to return to the previous screen with kotlin and java!
I want to send an email in Java.
I want to use java8 forEach with index
rsync4j --I want to touch rsync in Java.
I want to perform aggregation processing with spring-batch
I want to get the value in Ruby
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to get the IP address when connecting to Wi-Fi in Java
I want to ForEach an array with a Lambda expression in Java
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I want to dark mode with the SWT app
I want to transition screens with kotlin and java!
I tried to implement the Euclidean algorithm in Java
I want to get along with Map [Java beginner]
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.
I want to change the path after new registration after logging in with multiple devises.
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to perform high-speed prime factorization in Ruby (ABC177E)
I want to use the Java 8 DateTime API slowly (now)
I want to transition to the same screen in the saved state
I want to implement various functions with kotlin and java!
I want to pass the startup command to postgres with docker-compose.
[Java] I want to test standard input & standard output with JUnit
I tried to interact with Java
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
In Java, I want to trim multiple specified characters from only the beginning and end.
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I want to recreate the contents of assets from scratch in the environment built with capistrano
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
I want to change the value of Attribute in Selenium of Ruby
I want to perform asynchronous processing and periodic execution with Rail !!!
How to encrypt and decrypt with RSA public key in Java
[Android] I want to get the listener from the button in ListView
Java reference to understand in the figure
I want to use DBViewer with Eclipse 2018-12! !!
I tried the new era in Java
[Java] I want to check that the elements in the list are null or empty [Collection Utils]
How to get the date in java
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I went to the Java Women's Club # 1
[Ruby] I want to output only the odd-numbered characters in the character string
I want to display images with REST Controller of Java and Spring!
I want to select multiple items with a custom layout in Dialog
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
[Ruby] I want to extract only the value of the hash and only the key
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I want to display a PDF in Chinese (Korean) with thin reports
I want to perform Group By processing with Stream (group-by-count, group-by-sum, group-by-max)
I want to display an error message when registering in the database