How to remove duplicates from list in java
What you will learn here about java
- How to remove duplicates from list in java
One of the common interview question is how to remove duplicates from list in java. Here we will see how to remove duplicates from list in java. The simplest way to remove duplicates from list is use Stream API. So here we will see how to remove duplicates from list using Stream API.
How to remove duplicates from list in java
The example program for removing duplicates from list is given below.
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ListRemoveDuplicates { public static void main(String[] args) { Listlist=new ArrayList (); list.add(10); list.add(30); list.add(20); list.add(10); list.add(100); System.out.println("Before Stream operation -> "+list); List DistinctList=list.stream().distinct().collect(Collectors.toList()); System.out.println("After Stream operation ->"+DistinctList); } }
OUTPUT
Before Stream operation -> [10, 30, 20, 10, 100]
After Stream operation ->[10, 30, 20, 100]