List to Set in Java

Given a list (ArrayList or LinkedList), convert it into a set (HashSet or TreeSet) of strings in Java.

Method 1 (Simple)
We simply create an list. We traverse the given set and one by one add elements to the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate conversion of
// list to set using simple traversal
import java.util.*;
  
class Test {
    public static void main(String[] args)
    {
  
        // Creating a list of strings
        List<String> aList = Arrays.asList("Geeks", "for",
                     "GeeksQuiz", "GeeksforGeeks", "GFG");
  
        Set<String> hSet = new HashSet<String>();
        for (String x : aList)
            hSet.add(x);
  
        System.out.println("Created HashSet is");
        for (String x : hSet)
            System.out.println(x);
  
        // We can created TreeSet same way
    }
}

chevron_right


Method 2 (Using HashSet or TreeSet Constructor)



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate conversion of
// list to set using constructor
import java.util.*;
  
class Test {
    public static void main(String[] args)
    {
  
        // Creating a list of strings
        List<String> aList = Arrays.asList("Geeks", "for",
                     "GeeksQuiz", "GeeksforGeeks", "GFG");
  
        // Creating a hash set using constructor
        Set<String> hSet = new HashSet<String>(aList);
  
        System.out.println("Created HashSet is");
        for (String x : hSet)
            System.out.println(x);
  
        System.out.println("Created TreeSet is");
        Set<String> tSet = new TreeSet<String>(aList);
        System.out.println("Created TreeSet is");
        for (String x : tSet)
            System.out.println(x);
    }
}

chevron_right


Method 3 (Using addAll method)

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 list of strings
        List<String> aList = Arrays.asList("Geeks", "for",
                    "GeeksQuiz", "GeeksforGeeks", "GFG");
  
        Set<String> hSet = new HashSet<String>(aList);
        hSet.addAll(aList);
        System.out.println("Created HashSet is");
        for (String x : hSet)
            System.out.println(x);
  
        Set<String> tSet = new TreeSet<String>(aList);
        tSet.addAll(aList);
        System.out.println("Created TreeSet is");
        for (String x : tSet)
            System.out.println(x);
    }
}

chevron_right


Method 4 (Using stream in Java)
We use stream in Java to convert given list to stream, then stream to set. This works only in Java 8 or versions after that.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 list of strings
        List<String> aList = Arrays.asList("Geeks", "for",
                    "GeeksQuiz", "GeeksforGeeks", "GFG");
  
        // Converting to set using stream
        Set<String> set = aList.stream().collect(Collectors.toSet());
  
        for (String x : set)
            System.out.println(x);
    }
}

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.