

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to remove duplicates elements from a List
To remove duplicates from a List, the code is as follows −
Example
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.Set; public class Demo { public static void main(String[] args){ ArrayList<String> list = new ArrayList<String>(); list.add("Jacob"); list.add("Gary"); list.add("Gary"); list.add("Harry"); list.add("Harry"); list.add("Kevin"); System.out.println("List = "+list); Set<String> set = new LinkedHashSet<String>(list); System.out.println("List after removing duplicate elements = "+set); } }
Output
List = [Jacob, Gary, Gary, Harry, Harry, Kevin] List after removing duplicate elements = [Jacob, Gary, Harry, Kevin]
Example
Let us now see another example −
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Demo { public static void main(String[] args){ List<String> list = new ArrayList<String> (Arrays.asList("Ryan","Kevin","Ryan","Harry","Mark","Mark")); System.out.println("ArrayList = "+list); List<String> list2 = list.stream() .distinct() .collect(Collectors.toList()); System.out.println("Updated ArrayList without duplicates = "+list2); } }
Output
ArrayList = [Ryan, Kevin, Ryan, Harry, Mark, Mark] Updated ArrayList without duplicates = [Ryan, Kevin, Harry, Mark]
- Related Questions & Answers
- Python program to remove Duplicates elements from a List?
- Java Program to Remove Duplicates from an Array List
- Remove duplicates from a List in C#
- Python - Ways to remove duplicates from list
- C++ Program to Remove Duplicates from a Sorted Linked List Using Recursion
- C# program to remove duplicate elements from a List
- Python Program to Remove Palindromic Elements from a List
- Java program to remove all duplicates words from a given sentence
- Java program to print duplicates from a list of integers
- Remove Duplicates from Sorted List in C++
- Java Program to Remove a Sublist from a List
- Remove Duplicates from Sorted List II in C++
- How to remove duplicates from a sorted linked list in android?
- Python program to remove duplicate elements from a Circular Linked List
- Java Program to Remove elements from the LinkedList
Advertisements