In this post, we will learn java set to array conversion.
Table of Contents
1. Using Java 8’s Stream
If you are using Java 8, I would recommend using Java 8 Stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> s = new HashSet<String>(); s.add("John"); s.add("Martin"); s.add("Mary"); String[] arr = s.stream().toArray(String[] ::new); System.out.println(Arrays.toString(arr)); } } |
Output
2. Using toArray()
We can directly call toArray()
method on set object for java set to array conversion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> set = new HashSet<String>(); set.add("John"); set.add("Martin"); set.add("Mary"); String array[] = new String[set.size()]; array = set.toArray(array); System.out.println(Arrays.toString(array)); } } |
Output
3. Using toArray(IntFunction) [Java 11]
You can use Set#toArray(IntFunction<T[]>)
method which takes IntFunction
as generator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package package org.arpit.java2blog.entry; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> set = new HashSet<String>(); set.add("John"); set.add("Martin"); set.add("Mary"); String[] array = set.toArray(String[]::new); System.out.println(Arrays.toString(array)); } } |
Output
4. Using System.arraycopy()
We can use System.arraycopy()
for java set to array conversion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> set = new HashSet<String>(); set.add("John"); set.add("Martin"); set.add("Mary"); String array[] = new String[set.size()]; System.arraycopy(set.toArray(), 0, array, 0, set.size()); System.out.println(Arrays.toString(array)); } } |
Output
5. Using Arrays.copyOf
We can use Arrays.copyOf()
for java set to array conversion.
Here, Array.copyOf takes 3 parameters
1 2 3 4 5 6 7 8 |
* * @param Array which we need to copy * @param newLength: the length of the copy to be returned * @param newType: the class of the copy to be returned * @return a copy of the original array with specified newLength and newType public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> set = new HashSet<String>(); set.add("John"); set.add("Martin"); set.add("Mary"); String array[] = new String[set.size()]; System.arraycopy(set.toArray(), 0, array, 0, set.size()); System.out.println(Arrays.toString(array)); } } |
Output
6. Using simple iteration
Iterate through set and put the value in array manually.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> set = new HashSet<String>(); set.add("John"); set.add("Martin"); set.add("Mary"); String array[] = new String[set.size()]; int i = 0; for (String x : set) { array[i++] = x; } System.out.println(Arrays.toString(array)); } } |
Output
7. Using Guava library
There are two ways to convert set to array using guava library.
7.1 Using FluentIterable
FluentIterable
class provide similar functionality to Java 8’s Stream. It takes iterable set and returns an array which contains all the elements from fluent iterable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import com.google.common.collect.FluentIterable; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> set = new HashSet<String>(); set.add("John"); set.add("Martin"); set.add("Mary"); String[] array = FluentIterable.from(set).toArray(String.class); System.out.println(Arrays.toString(array)); } } |
Output
7.2 Using Iterables
Iterables
class provides toArray
method which takes set and newType as input and return array contains iterable’s elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import com.google.common.collect.Iterables; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class SetToArrayMain { public static void main(String[] args) { // Creating a new HashSet Set<String> set = new HashSet<String>(); set.add("John"); set.add("Martin"); set.add("Mary"); String[] array = Iterables.toArray(set, String.class); System.out.println(Arrays.toString(array)); } } |
Output
That’s all about Java set to array.