Java Program to convert a List to Set – 2 ways

Hello programmers, in this tutorial you are going to learn Java Program to convert a List to a Set. List and set both interface extends the collection framework.

The Set interface can be implemented using ArrayList, LinkedList, etc. The List interface can be implemented using HashSet, LinkedHashSet, etc.

In this post, you will learn two ways of converting List to Set.

Difference between List And Set Interface

  • The List interface maintains the insertion order of elements while the Set interface does not maintain any order.
  • The List can have duplicate elements while the Set doesn’t allow duplicate elements.

Java Program to convert a List to Set (1st  way)

  • First, you have to import three packages i.e.
import java.util.*;   import java.util.stream.*;   import java.util.function.Function;
  • After that, you need to create two methods one is the main method and the other is converListToSet.
    • public static void main(String args[])
    • public static <T> Set<T> convertListToSet(List<T> list1)

The main method

  • First, you need to create an object of the list interface for string and use Arrays.asList which makes the original array available as a list and specifies multiple strings to it. After that, print that list by typing System.out.println("List: " + list1); .
  • Then create a Set object named set2 of strings and store the returned value of the method convertListToSet().
  • While calling the convertListToSet method pass your current list as the argument in it.
  • Now print ” Converting List to Set ” and then subsequently after it print the set1 object elements. for eg:-  System.out.println("\n Converting List to Set \n");   System.out.println("Set from List: " + set1); 

Code for the main method

public static void main(String args[]) 
    { 
  
        
      List<String> list1 = Arrays.asList("Hello","Java","Is","A","Good","Programming","Language","Java"); 
  

        System.out.println("List: " + list1); 
  
        Set<String> set1 = convertListToSet(list1); 
        System.out.println("\n Converting List to Set \n"); 
        System.out.println("Set from List: " + set1); 
    }

The convertListToSet Method

  • First, create a method named convertListToSet with parameter as list1 of type List<T> and the method return type Set<T>.

public static <T> Set<T> convertListToSet(List<T> list1) .

  • Then you have to create an object of HashSet of Set as set1.
Set<T> set1 = new HashSet<>();
  • Then you have to add all the list elements of strings that are received by the method in the parameter to set1 by using set1.add method. By using an enhanced for loop that will traverse through every string element of the list while adding it to set1.
for (T t : list1) 

set1.add(t);
  • Now you need to return the set1 object from this method containing all the list of Strings that are converted into set.
return set1;
  • In conclusion:- the main method will receive this set1 object of strings and store it in the set interface variable which is declared in the main method and will print the set.


Source Code For Java Program to convert a List to Set

import java.util.*; 
import java.util.stream.*; 
import java.util.function.Function; 
  
public class ListToSet1 { 

 public static <T> Set<T> convertListToSet(List<T> list1) 
    { 
       
        Set<T> set1 = new HashSet<>(); 
  
        for (T t : list1) 
            set1.add(t); 
  
        return set1; 
    } 
  
 public static void main(String args[]) 
    { 
  
        
      List<String> list1 = Arrays.asList("Hello","Java","Is","A","Good","Programming","Language","Java"); 
  

        System.out.println("List: " + list1); 
  
        Set<String> set1 = convertListToSet(list1); 
        System.out.println("\n Converting List to Set \n"); 
        System.out.println("Set from List: " + set1); 
    } 
}

OUTPUT:

List: [Hello, Java, Is, A, Good, Programming, Language, Java]

 Converting List to Set

Set from List: [Java, A, Hello, Language, Programming, Is, Good]



Java Program to convert a List to Set (2nd  way)

In this method, you will use the HashSet constructor to convert List to Set.

  • First, you have import two packages i.e.
import java.util.*; 
import java.util.stream.*;
  • Now you need to create two methods:
    • public static void main(String args[])
    • public static <T> Set<T> convertListToSet(List<T> list1)

The main method

In the main method, you just need to follow the same steps as written in the 1st way of the main method above.

The code for the main method

public static void main(String args[]) 
   { 
 
List<String> list1 = Arrays.asList("Hello","Java","Is","A","Good","Programming","Language","Java"); 
  
       System.out.println("List: " + list1);

       Set<String> set1 = convertListToSet(list1); 
       System.out.println("\n Converting List to Set \n");        
       System.out.println("Set from List: " + set1); 
   }

 

The convertListToSet method

In this method, you have to directly write the return keyword and you basically have to write a new HashSet and call the HashSet constructor by passing the list that is received by the method into it. The HashSet constructor can take another collection object to construct a new set containing the elements of the specified list.

 public static <T> Set<T> convertListToSet(List<T> list1) 
{ 
return new HashSet<>(list1); 
}
  • In conclusion:- the main method will receive this HashSet object of strings and store it in the set interface variable which is declared in the main method and will print the set.

Source Code For Java Program to convert a List to Set

import java.util.*; 
import java.util.stream.*; 
  
public class ListToSet2{ 
 
    public static <T> Set<T> convertListToSet(List<T> list1) 
    { 
        return new HashSet<>(list1); 
    } 
  
    public static void main(String args[]) 
    { 
  
 List<String> list1 = Arrays.asList("Hello","Java","Is","A","Good","Programming","Language","Java"); 
   
        System.out.println("List: " + list1);
 
        Set<String> set1 = convertListToSet(list1); 
        System.out.println("\n Converting List to Set \n");        
        System.out.println("Set from List: " + set1); 
    } 
}

 

OUTPUT:

List: [Hello, Java, Is, A, Good, Programming, Language, Java]

 Converting List to Set

Set from List: [Java, A, Hello, Language, Programming, Is, Good]

 


 

Also, read:

Two trees are identical or not in Java

Leave a Reply

Your email address will not be published.