Convert Set (HashSet) to array example shows how to convert Set to array in Java using various ways including the toArray method and Java stream.
How to convert Set (HashSet) to array in Java?
There are a couple of ways using which you can convert Set to array in Java as given below.
1) Using the toArray method of the HashSet
1 |
public <T> T[] toArray(T[] array) |
This method returns an array containing all the elements of the collection. The type of the returned array is the same as that of the argument array.
The below given example shows how to convert HashSet of String to a String array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javacodeexamples.collectionexamples; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class ConvertSetToArrayExample { public static void main(String[] args) { Set<String> setDays = new HashSet<String>(); setDays.add("Sunday"); setDays.add("Monday"); setDays.add("Tuesday"); String[] strDays = setDays.toArray(new String[ setDays.size() ]); System.out.println( Arrays.toString(strDays) ); } } |
Output
1 |
[Monday, Sunday, Tuesday] |
If the argument array is smaller in size than the set object, a new array of the same type is created, filled with the set elements and returned. If the argument array is bigger than the set, the array element that immediately comes after the set elements is set to null.
1 2 3 4 5 6 7 8 9 10 |
Set<String> setDays = new HashSet<String>(); setDays.add("Sunday"); setDays.add("Monday"); setDays.add("Tuesday"); String[] strDays = new String[]{"1", "2", "3", "4", "5", "6"}; strDays = setDays.toArray(strDays); System.out.println( Arrays.toString(strDays) ); |
Output
1 |
[Monday, Sunday, Tuesday, null, 5, 6] |
As you can see from the output, the array element “4” is set to null in the array to mark the end of the set elements.
However, do not rely on the null array element to determine the boundary of the set elements as the set itself might contain the null elements as given below.
1 2 3 4 5 6 7 8 9 10 |
Set<String> setDays = new HashSet<String>(); setDays.add("Sunday"); setDays.add("Monday"); setDays.add(null); String[] strDays = new String[]{"1", "2", "3", "4", "5", "6"}; strDays = setDays.toArray(strDays); System.out.println( Arrays.toString(strDays) ); |
Output
1 |
[null, Monday, Sunday, null, 5, 6] |
Note: It is suggested to pass the argument array of the same size to the toArray
method. If the array is smaller than the size of the HashSet, a new array needs to be created by JVM to fit the collection elements and thus may impact the performance.
2) Using Java 8 stream
If you are using Java 8, you can create a stream from an array and then call the toArray
method to convert HashSet to an array as given below.
1 2 3 4 5 6 7 8 |
Set<String> setDays = new HashSet<String>(); setDays.add("Sunday"); setDays.add("Monday"); setDays.add("Tuesday"); String[] strDays = setDays.stream().toArray(String[]::new); System.out.println( Arrays.toString(strDays) ); |
Output:
1 |
[Monday, Sunday, Tuesday] |
This example is a part of the Java Array tutorial and Java HashSet tutorial.
Please let me know your views in the comments section below.