convert Array to Set and Set to array in Java



In this tutorial we will learn how to convert Array to Set  and how to convert Set to array in Java with example and programs.



How to convert  Array to Set >
  • Program/Example 1.1 to convert String Array to Set in java>
  • Program/Example 1.2  to convert Integer Array to Set in java >


How to convert  HashSet to array in Java >
  • Program/Example 2.1 to convert HashSet to String array  in java>
  • Program/Example 2.2 to convert HashSet to Integer array in java >

Before moving any further into this post I will like you to know bit more about List hierarchy in java - Detailed - ArrayList, LinkedList, vector classes


Set hierarchy in java - Detailed - HashSet, LinkedHashSet, TreeSet, ConcurrentSkipListSet, EnumSet classes




How to convert  Array to Set >
Program/Example 1.1 to convert String Array to Set in java>


First, convert Array to List, than
pass list in constructor of HashSet
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ConvertStringArrayToSetExample {
public static void main(String[] args) {
     String stringArray[] = { "a", "b", "a" };
     //First, convert Array to List
     //Than pass list in constructor of HashSet
     Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
     // Display set
     System.out.println(set);
}
}
/*OUTPUT
[a, b]
*/


Set does not allows Duplicate elements, so all the duplicate elements were removed.


Program/Example 1.2  to convert Integer Array to Set in java >


First, convert Array to List, Than
pass list in constructor of HashSet


import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ConvertIntegerArrayToSetExample {
public static void main(String[] args) {
     Integer integerArray[] = { 1, 2, 1 };
    
     //First, convert Array to List
     //Than pass list in constructor of HashSet
     Set<Integer> set = new HashSet<Integer>(Arrays.asList(integerArray));
     // Display set
     System.out.println(set);
}
}
/*OUTPUT
[1, 2]
*/


Set does not allows Duplicate elements, so all the duplicate elements were removed.

So far in this tutorial we learned how to convert  Array to Set  and  Now we will learn how to convert Set to array in Java with example and programs.

How to convert  HashSet to array in Java >


Program/Example 2.1 to convert HashSet to String array  in java>


First, convert set to List by passing set in constructor of ArrayList
Create stringArray of ArrayList's size
Use toArray method to convert ArrayList to Array


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ConvertHashSetToStringArrayExample {
public static void main(String[] args) {
     Set<String> set = new HashSet<String>();
     set.add("a");
     set.add("b");
    
     //First, convert set to List
     //by passing set in constructor of ArrayList
     List<String> arrayList = new ArrayList<String>(set);
    
     //Create stringArray of ArrayList's size
     String[] stringArray = new String[arrayList.size()];
     //Use toArray method to convert ArrayList to Array
     arrayList.toArray(stringArray);
    
     //Let's display the stringArray
     System.out.println("Display the stringArray");
     for(int i=0; i<stringArray.length; i++){
          System.out.print(stringArray[i]+" ");
     }
    
}
}
/*OUTPUT
Display the stringArray
a b
*/

Program/Example 2.2 to convert HashSet to Integer array in java >


First, convert set to List by passing set in constructor of ArrayList
Create integerArray of ArrayList's size
Use toArray method to convert ArrayList to Array


package convertArrayTo_set;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ConvertHashSetToIntegerArrayExample {
public static void main(String[] args) {
     Set<Integer> set = new HashSet<Integer>();
     set.add(1);
     set.add(2);
     //First, convert set to List
     //by passing set in constructor of ArrayList
     List<Integer> arrayList = new ArrayList<Integer>(set);
    
     //Create integerArray of ArrayList's size
     Integer[] integerArray = new Integer[arrayList.size()];
     //Use toArray method to convert ArrayList to Array
     arrayList.toArray(integerArray);
    
     //Let's display the integerArray
     System.out.println("Display the integerArray");
     for(int i=0; i<integerArray.length; i++){
          System.out.print(integerArray[i]+" ");
     }
    
}
}
/*OUTPUT
Display the integerArray
1 2
*/


So, In this tutorial we learned how to convert  Array to Set  and  how to convert Set to array in Java with example and programs.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


List hierarchy in java - Detailed - ArrayList, LinkedList, vector, CopyOnWriteArrayList classes



Set hierarchy in java - Detailed - HashSet, CopyOnWriteArraySet, LinkedHashSet, TreeSet, ConcurrentSkipListSet, EnumSet classes



Collection in java

Collection - List, Set and Map all properties in tabular form



eEdit
Must read for you :