- 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
Java Program to convert a List to a Set
To convert a List to a Set, let us first create a list and add elements:
List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50);
Now, let us convert the above list to a set with HashSet:
Set<Integer> set = new HashSet<Integer>(list);
Example
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Demo { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); list.add(20); list.add(40); list.add(50); Set<Integer> set = new HashSet<Integer>(list); System.out.println("Set = "); for (Object ob : set) System.out.println(ob); } }
Output
Set = 50 20 40 10 30
- Related Articles
- Program to convert Set to List in Java
- How to convert a Java list to a set?
- Convert a List to a Set in Java
- Java Program to convert a list to a read-only list
- Can we convert a list to a Set in Java?
- Golang Program to Convert List to Set
- C++ Program to Convert List to Set
- Java program to convert a Set to an array
- Convert List to Set in Java
- Java program to convert a list to an array
- Java program to convert an array to a list
- Program to convert a Vector to List in Java
- Convert a Set into a List in Java
- Python program to convert a list to a set based on a common element
- Java Program to convert a set into an Array

Advertisements