

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you create a list from a set in Java?
We can create a list from a set using its constructor.
List<Integer> list = new ArrayList<Integer>(set);
Example
Following is the example showing the conversion of set to list −
package com.tutorialspoint; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class CollectionsDemo { public static void main(String[] args) { Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); set.add(3); set.add(4); System.out.println("Set: " + set); List<Integer> list = new ArrayList<>(set); System.out.println("List: " + list); } }
Output
This will produce the following result −
Set: [1, 2, 3, 4] List: [1, 2, 3, 4]
- Related Questions & Answers
- How do you create a list in Java?
- How do you turn a list into a Set in Java?
- How do you create a list with values in Java?
- How do you copy a list in Java?
- How do you create an empty list in Java?
- How do you make a list iterator in Java?
- How do you make a shallow copy of a list in Java?
- How do you turn an ArrayList into a Set in Java?
- How do you create a date object from a date in Swift xcode?
- How do you check a list contains an item in Java?
- How do you add an element to a list in Java?
- How do you prevent a method from getting overridden in java?
- How do you create a clickable Tkinter Label?
- How do you create a date object from a date in Swift xcode in iOS?
- How do you create a Button on a Tkinter Canvas?
Advertisements