In the past, I have shared example of key Stream methods like map, filter, findAny, flatMap, collect, and in this example, I will explain you how and when to use distinct method of Stream class in Java.
Java Stream.distinct() method Example
public static void main(String[] args) {
List<Integer> list = Arrays.asList(2, 3, 4, 3, 2, 3, 4, 1, 2, 4, 5, 1);
list.stream().distinct().forEach(System.out::println);
}
2
3
4
1
5
In this example, I am just printing the values to console but after calling distinct() you get another Stream with unique values and you can do whatever you want to do with that. For example, you can also collect into another List to create a list of unique elements.
If you also want the previous output to be sorted, you can also use stream().distinct().sorted() which sorts the output after distinct returns the unique values in the list.
public static void main(String[] args) {
List<Integer> list = Arrays.asList(2, 3, 4, 3, 2, 3, 4, 1, 2, 4, 5, 1);
list.stream().distinct().sorted().forEach(System.out::println);
}
The Output:
1 2 3 4 5
In general, Stream distinct preserves the order of the selected collection if it is ordered already, if not preservation of the order is not guarantee.
Moreover, let’s see how we can use Stream distinct in other efficient way, like getting the number of the redundant data of the collection
For example:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(2, 3, 4, 3, 2, 3, 4, 1, 2, 4, 5, 1);
System.out.println("How many numbers are repeated? ");
System.out.println(list.stream().distinct().count());
}
The Output:
How many numbers are repeated?
5
If you are a visual learner, here is a nice diagram which explains how distinct() method filter out duplicate or repeated elements from Stream in Java:
That's all about the Stream.distinct() method in Java. Much like SQL, where distinct keyword is used to eliminate or filter duplicates, you can use distinct() to filter or remove duplicate elements from Stream. This is quite useful while going over a list which may contain duplicates.
Actually Stream API in Java is quite inspired with SQL, you not only have distinct() but you also have group by (Collectors.groupingBy()) and partition by which you can use with Collectors. I highly recommend you to check those tutorial to learn more about these exciting and powerful functions from Stream API in Java.
Other Java 8 tutorials you may like
- How to sort the may by values in Java 8? (example)
- Difference between map() and flatMap in Java 8 (answer)
- 15 Java Stream and Functional Programming interview questions (list)
- 10 Courses to learn Java in-depth (courses)
- How to join String in Java 8 (example)
- 5 Books to Learn Java 8 from Scratch (books)
- Difference between abstract class and interface in Java 8? (answer)
- Java 8 Predicate and Lambda Expression example (predicate example)
- What is the default method in Java 8? (example)
- 20 Examples of Date and Time in Java 8 (tutorial)
- How to sort the map by keys in Java 8? (example)
- How to format/parse the date with LocalDateTime in Java 8? (tutorial)
- How to convert List to Map in Java 8 (solution)
- How to use Stream class in Java 8 (tutorial)
- 10 examples of Optional in Java 8? (example)
Thanks for reading this Java Stream tutorial so far. If you like this Java Stream Tutorial and distinct example then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
The answer for "How many numbers are repeated?" is 4 not 5.
ReplyDelete