Set to List in Java
Given a set (HashSet or TreeSet) of strings in Java, convert it into a list (ArrayList or LinkedList) of strings.
Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : ArrayList or Linked List with following content {"Geeks", "for"}
Method 1 (Simple)
We simply create an list. We traverse the given set and one by one add elements to the list.
// Java program to demonstrate conversion of // Set to array using simple traversal import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add( "Geeks" ); s.add( "for" ); int n = s.size(); List<String> aList = new ArrayList<String>(n); for (String x : s) aList.add(x); System.out.println( "Created ArrayList is" ); for (String x : aList) System.out.println(x); // We can created LinkedList same way } } |
Created ArrayList is Geeks for
Method 2 (Using ArrayList or LinkedList Constructor)
// Java program to demonstrate conversion of // Set to list using constructor import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add( "Geeks" ); s.add( "for" ); // Creating an array list using constructor List<String> aList = new ArrayList<String>(s); System.out.println( "Created ArrayList is" ); for (String x : aList) System.out.println(x); System.out.println( "Created LinkedList is" ); List<String> lList = new LinkedList<String>(s); for (String x : lList) System.out.println(x); } } |
Created ArrayList is Geeks for Created LinkedList is Geeks for
Method 3 (Using addAll method)
// Java program to demonstrate conversion of // Set to array using addAll() method. import java.util.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add( "Geeks" ); s.add( "for" ); List<String> aList = new ArrayList<String>(); aList.addAll(s); System.out.println( "Created ArrayList is" ); for (String x : aList) System.out.println(x); List<String> lList = new LinkedList<String>(); lList.addAll(s); System.out.println( "Created LinkedList is" ); for (String x : lList) System.out.println(x); } } |
Created ArrayList is Geeks for Created LinkedList is Geeks for
Method 4 (Using stream in Java)
We use stream in Java to convert given set to steam, then stream to list. This works only in Java 8 or versions after that.
// Java program to demonstrate conversion of // Set to list using stream import java.util.*; import java.util.stream.*; class Test { public static void main(String[] args) { // Creating a hash set of strings Set<String> s = new HashSet<String>(); s.add( "Geeks" ); s.add( "for" ); List<String> aList = s.stream().collect(Collectors.toList()); for (String x : aList) System.out.println(x); } } |
Geeks for
Recommended Posts:
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- List to Set in Java
- Min and Max in a List in Java
- How to Clone a List in Java?
- Initializing a List in Java
- Convert List to Set in Java
- List to array in Java
- List of all Java Keywords
- Immutable List in Java
- Program to Convert Set to List in Java
- List add(E ele) method in Java with Examples
- Sum of list with stream filter in Java
- Conversion of Java Maps to List
- Split a list into two halves in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.