
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- 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
- Java program to remove all duplicates words from a given sentence
- Java program to print duplicates from a list of integers
- C# program to remove duplicate elements from a List
- Python Program to Remove Palindromic Elements from a List
- Remove Duplicates from Sorted List in C++
- Java Program to Remove a Sublist from a List
- Remove Duplicates from a Sorted Linked List Using Recursion
- Golang program to remove elements from the linked list
- How to remove duplicates from a sorted linked list in android?
- Golang Program To Remove Duplicates From An Array
- Python program to remove duplicate elements from a Circular Linked List

Advertisements