- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a List to a Set in Java
In order to convert a List to a Set in Java, we can create an ArrayList and pass the List as an argument in the parameterized constructor of an HashSet. This can be done as follows −
List l = new ArrayList(); Set s = new HashSet(l);
Let us see a program to convert a list to a set in Java −
Example
import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.HashSet; public class Example { public static void main(String[] args) { List l = new ArrayList(); l.add("Good"); l.add("Morning"); l.add("Morning"); Set s = new HashSet(l); System.out.println(s); } }
Output
[Morning, Good]
- Related Articles
- Java Program to convert a List to a Set
- How to convert a Java list to a set?
- Convert a Set into a List in Java
- Can we convert a list to a Set in Java?
- Convert List to Set in Java
- Can we convert a List to Set and back in Java?
- Program to convert Set to List in Java
- What is the easiest way to convert a List to a Set in Java?
- How can we convert list to Set in Java?
- Convert a Queue to a List in Java
- Convert set into a list in Python
- Java Program to convert a list to a read-only list
- Convert an Iterator to a List in Java
- Program to convert a Vector to List in Java
- How to convert a list to array in Java?

Advertisements