
- 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
What is the easiest way to convert a List to a Set in Java?
There are couple of easy ways to convert a list to a set. If a new set to be created then using constructor is the easiest one. Just pass the list to the constructor and resultant set will contains all the unique elements of the list.
Set<String> set = new HashSet<>(list);
In case of existing set, we can use set.addAll() method to add all the elements of the list to the set.
set.addAll(list);
Another interesting way is with the use of streams, we can get a set from a list.
set = list.stream().collect(Collectors.toSet());
Example
Following is the example showing the list to set conversion via multiple ways −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,3,3,4,5)); System.out.println("List: " + list); Set<Integer> set = new HashSet<>(list); System.out.println("Set: " + set); set = new HashSet<>(); set.addAll(list); System.out.println("Set: " + set); set = new HashSet<>(); set = list.stream().collect(Collectors.toSet()); System.out.println("Set: " + set); } }
Output
This will produce the following result −
List: [1, 2, 3, 3, 3, 4, 5] Set: [1, 2, 3, 4, 5] Set: [1, 2, 3, 4, 5] Set: [1, 2, 3, 4, 5]
- Related Articles
- What is the easiest way to reverse a String in Java?
- What is the easiest way to convert int to string in C++
- Convert a List to a Set in Java
- Java Program to convert a List to a Set
- How to convert a Java list to a set?
- What is the easiest way to store date in MySQL database?
- What is the easiest way to lose weight without exercise?
- Can we convert a list to a Set in Java?
- What is the easiest way to initialize a std::vector with hardcoded elements in C++?
- What is the easiest way to transfer files from phone to PC?
- Convert List to Set in Java
- Convert a Set into a List in Java
- Program to convert Set to List in Java
- Can we convert a List to Set and back in Java?
- Easiest way to get number of rows in a MySQL table?

Advertisements