Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Remove Duplicate Values From List Using Java Stream API?

Posted on 04/23/2025 By admin

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.

If you want to learn from a video tutorial then please refer to the below video –

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.

  1. Create an empty list.
  2. Iterate the original list and add its element to the new list if the element is not present in the new list.
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 List listWithStringValues = 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); }
}
Actual Data : [Java, Selenium, Physics, Selenium]
After removing duplicate Data : [Java, Selenium, Physics]

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.

  1. Get a sequential stream of List using stream() method.
  2. Call distinct() method to remove all duplicates from the list.
  3. Collect results into a List.
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 List listWithStringValues = 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); } }
Actual Data : [Java, Selenium, Physics, Selenium]
After removing duplicate Data : [Java, Selenium, Physics]

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

Uncategorized

Post navigation

Previous Post: Git Tutorial 33 – How To Restore Deleted And Committed But Not Pushed File By GIT Reset?
Next Post: REST Assured Tutorial 39 – @JsonIgnore Annotation of Jackson API – Exclude Field of Pojo From Serialization and Deserialization

Related Posts

Manual Testing Uncategorized
July 2, 2017 – Make Selenium Easy Uncategorized
September 23, 2018 – Make Selenium Easy Uncategorized
Core Java Quiz – 1 – Variables Uncategorized
How to Query JSON using JSONPath? Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark