How do i Convert List to Set
Published May 20, 2022ArrayLists and HashSets are both supported in Java, and you may choose either based on your specific requirements. The two are quite distinct, and they are used for very different purposes.
The ArrayList is a List interface implementation class used to store data in linear order, while the Set is an interface that contains the HashSet class to store data. The primary distinction between these two is that the HashSet does not permit duplicate entries and only stores unique elements.
This article will go through several methods for converting an ArrayList to a HashSet.
How to convert a List into a Set
There are three main simple ways you can use to convert a List into a set in Java, they include:
? Using Naive Approach
? Using constructors
? Using Java 8 Stream API
Method 1: Using the Naive Approach
This is one of the most common methods for converting a list to a set, and it is preferred by the majority of developers, particularly novices. This method is really straightforward, and it entails the following basic steps:
- Step 1: To begin, create an empty set.
- Step 2: Iterate over the list, adding each item to the Set.
- Step 3: Finally, use the HashSet to add the set components.
Now, let's use these steps in the example below.
Example
In this example, we'll make a list and save the names of numerous programming IDEs. We also create an empty set called Set.
Then we loop over the list, adding each element to the set one by one. Finally, we print the pieces in the Set
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set;
public class Convert_Java_to_List_method1 { public static void main(String[] args) { List<String> IDElist = Arrays.asList("Android Studio", "Ecclipse", "Netbeans", "Sublime Text"); Set<String> set = new HashSet<String>(); for(String ele : IDElist){ set.add(ele); } for(String ele : set){ System.out.println(ele); }
}
} |
Output:
Netbeans Sublime Text Android Studio Ecclipse |
Method 2: Using constructors
This is another simple method you might do. In this method, you simply need to pass the list that needs to be turned into a HashSet constructor. The constructor then returns a new set containing all of the unique elements. Now consider the following example
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set;
public class Convert_Java_to_List_method2 {
public static void main(String[] args) { List<String> IDElist = Arrays.asList("Android Studio", "Pycham", "Notepad++", "Dreamweaver"); Set<String> IDEset = new HashSet<String>(IDElist); for(String ele : IDEset){ System.out.println(ele); }
}
} |
Output
Pycham Notepad++ Dreamweaver Android Studio |
In this example, the constructor has removed the necessity for the for loop, which was utilized in the prior approach (method 1).
Method 3: Using Java 8 Stream API
This is the last technique we'll look at in this article. We will employ the Java 8 Stream API to transform the ArrayList into a HashSet. First, we'll transform the list into a stream, and then we'll add a function to the stream.
We will use the Collectors.toSet() method in the collect process to convert it into a set. Take a look at the code below
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.Set;
public class Convert_Java_to_List_method3 {
public static void main(String[] args) { List<String> IDElist = Arrays.asList("Netbeans", "Android studio", "Notepad ++", "Sublime Text"); Set<String> set = IDElist.stream().collect(Collectors.toSet()); for(String ele : set){ System.out.println(ele); }
}
} |
Output:
Netbeans Android studio Notepad ++ Sublime Text |
Keywords: How to Convert a List into a Set , Java Collections, Java
Article Contributed By :
|
|
|
|
307 Views |