Wednesday, April 5, 2023

How to use Stream.distinct() to remove duplicates from List in Java? Tutorial

Hello guys, if you are working in Java then you may know that Stream in Java is used to perform certain operations on the objects elements like printing list’s elements, order them, and filter them by defined condition etc. While most of the Java developer only focus on core methods like map, flatMap, filter, and collect you should also explore the whole Stream API as it contains many gems which can help you to write better code, distinct() method from Stream class is one of them. Java Stream Distinct is used to get a stream having non repetitive elements. So it eliminates the redundant data using hashCode() and equals() which is useful to compare objects together.

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. 

Let’s see an example of Stream.distinct() method to understand it better:

Java Stream.distinct() method Example

IAs we see the following list contains repetitive numbers or duplicates but I am only interested on unique values, so I am using stream().distinct() we get rid of those repetitive numbers or duplicates.

public static void main(String[] args) {

    List<Integer> list = Arrays.asList(
234323412451);

    
list.stream().distinct().forEach(System.out::println);
}


The Output:

 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. 

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);
   
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:

Java Stream Distinct Example Tutorial


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.

P. S. - If you are new to Java and want to learn Stream API in depth, I also recommend you to checkout these Java Collections and Stream API online courses to learn Stream in depth. It is one of the most essential API and I think every Java developer must have solid knowledge of Stream and Collection to be a successful Java developer. 

1 comment:

  1. The answer for "How many numbers are repeated?" is 4 not 5.

    ReplyDelete