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. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.
Set in Java is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. Few important features of Java Set interface are as follows:
- The set interface is an unordered collection of objects in which duplicate values cannot be stored.
- The Java Set does not provide control over the position of insertion or deletion of elements.
- Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation).
- Set has various methods to add, remove clear, size, etc to enhance the usage of this interface.
Examples:
Input: Array: [Geeks, forGeeks, A computer Portal]
Output: Set: [Geeks, forGeeks, A computer Portal]Input: Array: [1, 2, 3, 4, 5]
Output: Set: [1, 2, 3, 4, 5]
Below are methods to convert Array to Set in Java:
- Brute Force or Naive Method: In this method, an empty Set is created and all elements present of the Array are added to it one by one.
Algorithm:
- Get the Array to be converted.
- Create an empty Set
- Iterate through the items in the Array.
- For each item, add it to the Set
- Return the formed Set
Program:
// Java Program to convert// Array to Setimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an Array to Setpublicstatic<T> Set<T> convertArrayToSet(T array[]){// Create an empty SetSet<T> set =newHashSet<>();// Iterate through the arrayfor(T t : array) {// Add each element into the setset.add(t);}// Return the converted Setreturnset;}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A Computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to SetSet<String>set = convertArrayToSet(array);// Print the SetSystem.out.println("Set: "+ set);}}chevron_rightfilter_noneOutput:Array: [Geeks, forGeeks, A computer Portal] Set: [Geeks, forGeeks, A computer Portal]
- Using Arrays.asArray() method: In this method, the Array is passed as the parameter into the Set constructor in the form of an Array with the help of Arrays.asArray() method.
Algorithm:
- Get the Array to be converted.
- Create the Set by passing the Array as parameter in the constructor of the Set with the help of Arrays.asArray() method
- Return the formed Set
Program:
// Java Program to convert// Array to Setimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an Array to Setpublicstatic<T> Set<T> convertArrayToSet(T array[]){// Create the Set by passing the Array// as parameter in the constructorSet<T> set =newHashSet<>(Arrays.asList(array));// Return the converted Setreturnset;}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to SetSet<String>set = convertArrayToSet(array);// Print the SetSystem.out.println("Set: "+ set);}}chevron_rightfilter_noneOutput:Array: [Geeks, forGeeks, A computer Portal] Set: [Geeks, forGeeks, A computer Portal]
- Using Collections.addAll(): Since Set is a part of the Collection package in Java. Therefore the Array can be converted into the Set with the help of Collections.addAll() method.
Algorithm:
- Get the Array to be converted.
- Create an empty Set.
- Add the array into the Set by passing it as the parameter to the Collections.addAll() method.
- Return the formed Set
Program:
// Java Program to convert// Array to Setimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an Array to Setpublicstatic<T> Set<T> convertArrayToSet(T array[]){// Create the Set by passing the Array// as parameter in the constructorSet<T> set =newHashSet<>();// Add the array to setCollections.addAll(set, Arrays.toString(array));// Return the converted Setreturnset;}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ array);// convert the Array to SetSet<String>set = convertArrayToSet(array);// Print the SetSystem.out.println("Set: "+ set);}}chevron_rightfilter_noneOutput:
Array: [Geeks, forGeeks, A computer Portal] Set: [Geeks, forGeeks, A computer Portal]
- Using Java 8 Stream API: HashSet constructor can take another collection object to construct a new set containing the elements of the specied array.
Algorithm:
- Get the Array to be converted.
- Convert the array to Stream
- Convert the Stream to Set using Collectors.toSet()
- Collect the formed set using collect() method
- Return the formed Set
Program:
// Java Program to convert// Array to Set in Java 8importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert array to setpublicstatic<T> Set<T> convertArrayToSet(T array[]){// create a set from the ArrayreturnArrays.stream(array).collect(Collectors.toSet());}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to SetSet<String>set = convertArrayToSet(array);// Print the SetSystem.out.println("Set: "+ set);}}chevron_rightfilter_noneOutput:
Array: [Geeks, forGeeks, A computer Portal] Set: [Geeks, forGeeks, A computer Portal]
- Using Guava Sets.newHashSet(): Sets.newHashSet() creates a mutable HashSet instance containing the elements of the specified array.
Algorithm:
- Get the Array to be converted.
- Create an empty Set.
- Add the array into the Set by passing it as the parameter to the Sets.newHashSet() method.
- Return the formed Set
// Java Program to convert// Array to Set in Java 8importstaticcom.google.common.collect.Sets.*;importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert array to setpublicstatic<T> Set<T> convertArrayToSet(T array[]){// create a set from the ArrayreturnSets.newHashSet(array);}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to SetSet<String>set = convertArrayToSet(array);// Print the SetSystem.out.println("Set: "+ set);}}chevron_rightfilter_noneOutput:
Array: [Geeks, forGeeks, A computer Portal] Set: [Geeks, forGeeks, A computer Portal]
Recommended Posts:
- Program to convert Array to List in Java
- Program to Convert Stream to an Array in Java
- Program to convert Primitive Array to Stream in Java
- Java Program to convert Character Array to IntStream
- Program to convert Byte Array to Writer in Java
- Program to convert Boxed Array to Stream in Java
- Program to Convert Set of Integer to Array of Integer in Java
- Program to convert a Map to a Stream in Java
- Program to Convert List to Map in Java
- Program to Convert Set to List in Java
- How to convert Vector to array in Java
- How to convert LinkedList to Array in Java?
- Program to convert String to IntStream in Java
- Program to convert set of String to set of Integer in Java
- Program to convert ArrayList to LinkedList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



