
- 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
How do you turn an ArrayList into a Set in Java?
An ArrayList can be converted to a set object using Set constructor. The resultant set will elliminate any duplicate entry present in the list and will contains only the unique values.
Set<String> set = new HashSet<>(list);
Or we can use set.addAll() method to add all the elements of the list to the set.
set.addAll(list);
Using streams as well, 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, 4, 4, 5, 5)); System.out.println("ArrayList: " + 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 −
ArrayList: [1, 2, 3, 4, 4, 5, 5] Set: [1, 2, 3, 4, 5] Set: [1, 2, 3, 4, 5] Set: [1, 2, 3, 4, 5]
- Related Articles
- How do you turn a list into a Set in Java?
- How do I turn a list into an array in Java?
- How do you convert an ArrayList to an array in Java?
- How do you search for an element in an ArrayList in Java?
- How to convert a comma separated String into an ArrayList in Java?
- How do you create a list from a set in Java?
- How do you convert a list collection into an array in C#?
- How do you add an element to a list in Java?
- How do you check a list contains an item in Java?
- How do you create an empty list in Java?
- How to reverse an ArrayList in Java?
- How to synchronize an ArrayList in Java?
- Turn an image into a Bootstrap 4 card background
- How do I turn a string in dot notation into a nested object with a value – JavaScript?
- Java Program to convert a set into an Array

Advertisements