In this article, we will discuss how to convert Arrays into Set
1. Conversion of Arrays into List using asList() method :
- We can use inter-conversion constructor of HashSet
- to convert Arrays into Set
- by passing Arrays as constructor-argument
Method signature:
public static List asList(Object[] oArray);
Syntax:
Set<String> set = new HashSet<String>(
Arrays.asList(arrayValues)
);
ConvertArraysIntoSet.java
package in.bench.resources.java.collections;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ConvertArraysIntoSet {
public static void main(String[] args) {
/*******************Integer Arrays example **************/
// Integer Arrays
Integer[] intArrays = {31, 83, 53, 97, 29, 7, 13, 47, 79};
// conversion of Integer Arrays to Set (through List)
SetSet<Integer> integerSet = new HashSetSet<Integer>(
Arrays.asList(intArrays));
// printing to console
System.out.println("Conversion of"
+ " Integer[] to Set<Integer>:\n\n"
+ integerSet);
/*******************String Arrays example **************/
// String Arrays
String[] strArrays = {
"James",
"Bond",
"Mike",
"Pups",
"Bird"
};
// conversion of String Arrays to Set (through List)
Set<String> strSet = new HashSet<String>(
Arrays.asList(strArrays));
// printing to console
System.out.println("\n\n\nConversion of"
+ " String[] to Set<String>:\n\n"
+ strSet);
}
}
Output:
Conversion of Integer[] to Set<Integer>:
[79, 31, 47, 13, 29, 7, 97, 53, 83]
Conversion of String[] to Set<String>:
[Pups, Bird, James, Bond, Mike]
Note:
- We can also convert Set into Arrays using Collection’s toArray() method
- Refer Conversion of HashSet to Arrays for example
Related Articles:
- Java – Conversion of List to Map
- Java – Conversion of Map to List
- Java – Conversion of Arrays to List
- Java – Conversion of List to Arrays
- Java 8 – Conversion of List to Map
- Java 8 – Conversion of Map to List
- Java 8 – Conversion of Arrays to List
- Java 8 – Conversion of List to Arrays
- Java 8 – How to convert HashMap to ArrayList ?
- Java – Conversion of ArrayList to Arrays in 2 ways
- Java – Conversion of Arrays to Vector
- Java – Conversion of Arrays to HashSet
- Java – Conversion of HashSet to Arrays
- Java – Conversion of LinkedList into Vector
- Java – Conversion of ArrayList into HashSet to remove duplicate elements
- Java – How to convert LinkedList to contain unique elements in ascending-order ?
- Java – Converting Ordered ArrayList into Sorted TreeSet
- Java – How to remove elements while iterating collection object ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/class-use/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
Happy Coding !!
Happy Learning !!