- 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
Can we convert a list to a Set in Java?
We can convert a list into a set easily using Set's constructor. We need to pass the list to the constructor.
Syntax
Set<String> set = new HashSet<String>(list);
In case, list contains the duplicate values, set will remove them and will keep only unique values.
Example
The following example shows how to convert a list into set.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "C")); // print the list System.out.println(list); Set<String> set = new HashSet<String>(list); System.out.println(set); } }
Output
This will produce the following result −
[A, B, C, C] [A, B, C]
- Related Articles
- Can we convert a List to Set and back in Java?
- How can we convert list to Set in Java?
- Can we convert a Java array to list?
- Can we convert a Java list to array?
- Can we convert a list to an Array in Java?
- Convert a List to a Set in Java
- How can we convert a list to the JSON array in Java?
- 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
- How can we convert a JSON array to a list using Jackson in Java?\n
- Convert List to Set in Java
- Can we convert an array to list and back in Java?
- Program to convert Set to List in Java
- Can we add null elements to a Set in Java?

Advertisements