Conversion Between an Array and a Set in Java
In this tutorial, we will see how to convert an array to set and Set to back an array using different methods.
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array.
Set is a collection that contains no duplicate elements. Set in Java is a part of java.util package and extends java.util.Collection interface.
It does not provide control over the position of insertion or deletion of elements.
Convert Array to Set
1). By Iterating Array
2). Using Arrays.asList() method
3). Using Collections.addAll()
4). Using Apache Commons Collections
1). By Iterating Array
In this approch we add each array element to set while Iterating the array.
import java.util. * ;
import java.util.stream. * ;
class ArrayToSet1 {
public static Set <String> convertArrayToSet(String array[]) {
Set <String> strSet = new HashSet <>();
for (String str: array) {
set.add(strSet);
}
return strSet;
}
public static void main(String args[]) {
String array[] = {
"Hello",
"World",
"Java",
"Hello",
System.out.println("Original Array is : " + Arrays.toString(array));
Set <String> stringSet = convertArrayToSet(array);
System.out.println("The Set from Array is : " + stringSet);
}
}
2). Using Arrays.asList() method
In this approch the array is passed as the parameter to the Set constructor in the form of an Array with the help of Arrays.asList() method.
import java.util.*; import java.util.stream.*; class ArrayToSet1 { public static Set <String> convertArrayToSet(String array[]) { Set <String> strSet = new HashSet <>(Arrays.asList(array)); return strSet; } public static void main(String args[]) { String array[] = { "Hello", "World", "Java", "Hello"}; System.out.println("Original Array is : " + Arrays.toString(array)); Set
stringSet = convertArrayToSet(array); System.out.println("The Set from Array is : " + stringSet); } }
3). Using Collections.addAll()
In this approch array converted to the Set by passing it as the parameter to the Collections.addAll() method.
import java.util. * ;
import java.util.stream. * ;
class ArrayToSet3 {
public static Set < String > convertArrayToSet(String array[]) {
Set <String> strSet = new HashSet <>();
Collections.addAll(strSet, Arrays.toString(array));
return strSet;
}
public static void main(String args[]) {
String array[] = {
"Hello",
"World",
"Java",
"Hello"};
System.out.println("Original Array is : " + Arrays.toString(array));
Set <String> stringSet = convertArrayToSet(array);
System.out.println("The Set from Array is : " + stringSet);
}
}
4). Using Apache Commons Collections
The Apache Commons Collections has CollectionUtils class contains addAll() method which takes empty Set and array as argumnet and convert array to Set.
public static Set <String> convertArrayToSet(String array[]) {
Set<String> strSet = new HashSet<>(array.size);
CollectionUtils.addAll(strSet, array);
return strSet;
}
In this article, we have seen How to Convert an Array to Set and viceversa.
0 Comments
Post a Comment