Introduction
Stream API feature was introduced in Java 8 but it is still confusing for many and also many have not even used it a single time. I will try to explain Java Stream concepts with simple examples in Learn Stream API in Java with Examples series.
In this post, we will learn to remove duplicates from a Java List using Stream APIs i.e. distinct() method.
YouTube Tutorials
If you want to learn from a video tutorial then please refer to the below video –
Remove Duplicate Values From List Without Stream API
There are multiple logics to remove duplicates from a List in Java. We will implement a simple logic here. Suppose we have a List of integer or string values.
- Create an empty list.
- Iterate the original list and add its element to the new list if the element is not present in the new list.
Java Program
package solveproblemswithsreams; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class RemoveDuplicateFromListUsingStreamAPI { public static void main(String[] args) { // Original List with duplicate values ListlistWithStringValues = Arrays.asList("Java","Selenium","Physics","Selenium"); System.out.println("Actual Data : "+ listWithStringValues); // Remove duplicates using without stream List listWithoutDuplicateValuesWithoutStream = new ArrayList<>(); for(String ele : listWithStringValues) { if(!listWithoutDuplicateValuesWithoutStream.contains(ele)) { listWithoutDuplicateValuesWithoutStream.add(ele); } } System.out.println("After removing duplicate Data : "+ listWithoutDuplicateValuesWithoutStream); } }
Output
Actual Data : [Java, Selenium, Physics, Selenium] After removing duplicate Data : [Java, Selenium, Physics]
Remove Duplicate Values From List With Stream API
There is nothing wrong with the above logic but Java stream APIs can help to eliminate all these headaches. It is always good to use predefined methods rather than writing your own logic until you are preparing for interviews. Let’s rewrite the program with the help of Stream APIs.
- Get a sequential stream of List using stream() method.
- Call distinct() method to remove all duplicates from the list.
- Collect results into a List.
Java Program
package solveproblemswithsreams; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class RemoveDuplicateFromListUsingStreamAPI { public static void main(String[] args) { // Original List with duplicate values ListlistWithStringValues = Arrays.asList("Java","Selenium","Physics","Selenium"); // Remove duplicates using with stream List listWithoutDuplicateValuesWithStream = listWithStringValues .stream() // To get a sequential stream .distinct() // To get unique elements .collect(Collectors.toList()); // After removing duplicate save or collect in to a list System.out.println("Actual Data : "+ listWithStringValues); System.out.println("After removing duplicate Data : "+ listWithoutDuplicateValuesWithStream); } }
Output
Actual Data : [Java, Selenium, Physics, Selenium] After removing duplicate Data : [Java, Selenium, Physics]
GitHub Repo Link
https://github.com/amod-mahajan/MSE_LearnJavaStreamsWithExamples.git
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning