Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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]
Advertisements