AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

Convert List to Set and Set to List

In this article, let’s explore various methods to convert Set to List and List to Set. In the following examples, we will see how we can achieve this using Java, Guava, and the Apache Commons Collection library.

What is Google Guava?

Google Guava is a set of open-source libraries for Java developed by Google.

What is Apache Commons Collections?

Apache Commons Collections, which has been an improvement introduced since Java version 1.2, adds enhancements for data structures.

Converting Set to List

In this example, we will start with a Java class that has a Main method for execution and a private method where the logic for Set to List transformations resides. Only this private method will be changed in different methods.

We are going to convert from Set to List in 3 different ways:

  • API de Java
  • Guava
  • Apache Commons

Using Java:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ConvertToListFromSet{
    public static void main( String[] args ){
    	
       convertToList();
    }

   private void convertToList() {
        System.out.println("Values .....");
    	Set<String> fromSet = Sets.newHashSet("Hi", "Hello", 
        "Bye", "See 
         you");       

        fromSet.forEach(s->System.out.println(s));

        List<String> toList = new ArrayList<>(sourceSet);

        toList.forEach(s->System.out.println(s));
   }
}

Output:

Values .....
Bye
Hello
Hi
See you

Bye
Hello
Hi
See you

With Guava

Now, we are going to convert to list with Guava.To see the next example, we will modify only the private method.

   private void convertToList() {
        System.out.println("Values .....");

    	Set<String> fromSet = Sets.newHashSet("Hi", "Hello", 
        "Bye", "See 
         you");       
 
        fromSet.forEach(s->System.out.println(s));

        List<String> toList =  Lists.newArrayList(sourceSet);

        toList.forEach(s->System.out.println(s));
   }

Using Apache Commons Collections

We are going to convert from Set to List with Apache Commons Collections. Similar to the previous case, we will perform the transformation only in the private method.

   private void convertToList() {
        System.out.println("Values .....");

    	Set<String> fromSet = Sets.newHashSet("Hi", "Hello", 
        "Bye", "See 
         you");       

        fromSet.forEach(s->System.out.println(s));

        List<Integer> toList = new ArrayList<>(4);
        CollectionUtils.addAll(targetList, sourceSet);
        
       toList.forEach(s->System.out.println(s));
   }

Converting List to Set

In this case we are going to convert from List to Set. Similar to the previous case, we will start with a Java class containing a main method and a private method where the logic for List to Set conversions resides. This class will display the values of both the Set and the List.

We are going to convert from List to Set in 3 different ways:

  • API de Java
  • Guava
  • Apache Commons

Using Java:

The transformation from List to Set using only Java would be as follows:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ConvertToListFromSet{
    public static void main( String[] args ){
    	
       convertToList();
    }

   private void convertToList() {
        System.out.println("Values .....");
    	List<String> fromList = Arrays.asList("Hi", "Hello", 
        "Bye", "See 
         you");       

        fromList.forEach(s->System.out.println(s)); 

        Set<String> toSet = new HashSet<>(sourceList);

        toSet.forEach(s->System.out.println(s));
   }
}

Output:

Values .....
Hi
Hello
Bye
See you

Bye
Hello
Hi
See you

Take in mind about Set: Does not maintain the order.

Using Guava

Next is an example to transform from List to Set with Guava:

   private void convertToList() {
        System.out.println("Values .....");
    	List<String> fromList = Lists.newArrayList("Hi", "Hello", 
        "Bye", "See 
         you");       

        fromList.forEach(s->System.out.println(s)); 

        Set<String> toSet = Sets.newHashSet(sourceList);

        toSet.forEach(s->System.out.println(s));
   }

Using Apache Commons Collections

Just like in the previous case, we only modify the private method using Apache Commons Collections.

   private void convertToList() {
        System.out.println("Values .....");
    	List<String> fromList = Lists.newArrayList("Hi", "Hello", 
        "Bye", "See 
         you");       

        fromList.forEach(s->System.out.println(s)); 

        Set<String> toSet = new HashSet<>(4);;
        CollectionUtils.addAll(toSet, fromList);

        toSet.forEach(s->System.out.println(s));
   }

Conclusion

In this brief article, we have seen three different ways to convert Set to List and List to Set. In this article, we have provided examples that, although covering basic concepts, are quite necessary when working with Java.

If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!

Leave a Reply

Your email address will not be published. Required fields are marked *