标签:Convert Set Java Arrays set new Integer array
Convert Set to array in Java
This post will discuss how to convert a set to an array in plain Java, Java 8, and the Guava library.
1. Naive solution
A naive solution is to iterate over the given set and copy each encountered element in the Integer array one by one.
1 2 3 4 5 6 7 8 9 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] array = new Integer[set.size()]; int k = 0; for (Integer i: set) { array[k++] = i; } System.out.println(Arrays.toString(array)); |
2. Using Set.toArray()
method
Set
interface provides the toArray()
method that returns an Object
array containing the elements of the set.
1 2 3 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Object[] array = set.toArray(); System.out.println(Arrays.toString(array)); |
The JVM doesn’t know the desired object type, so the toArray()
method returns an Object[]
. We can pass a typed array to the overloaded toArray(T[] a)
method to let JVM know your desired object type.
1 2 3 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] array = set.toArray(new Integer[set.size()]); System.out.println(Arrays.toString(array)); |
We can also pass an empty array of the specified type, and JVM will allocate the necessary memory:
1 2 3 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] array = set.toArray(new Integer[0]); System.out.println(Arrays.toString(array)); |
3. Using Java 8
In Java 8, we can use the Stream to convert a set to an array. The idea is to convert a given set to stream using Set.stream()
method and use Stream.toArray()
method to return an array containing the stream elements. There are two ways to do so:
⮚ Using Streams with method reference
1 2 3 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] array = set.stream().toArray(Integer[]::new); System.out.println(Arrays.toString(array)); |
⮚ Using Streams with lambda expression
1 2 3 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] array = set.stream().toArray(n -> new Integer[n]); System.out.println(Arrays.toString(array)); |
4. Using Guava Library
⮚ Using FluentIterable
class:
The FluentIterable
is an expanded Iterable
API that provides similar functionality as Java 8’s Stream. We can get a fluent iterable that wraps an iterable set and returns an array containing all the elements from the fluent iterable.
1 2 3 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] array = FluentIterable.from(set).toArray(Integer.class); System.out.println(Arrays.toString(array)); |
⮚ Using Iterables
class:
Guava’s Iterables
class also provides the toArray()
method that copies an iterable’s elements into an array.
1 2 3 | Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] array = Iterables.toArray(set, Integer.class); System.out.println(Arrays.toString(array)); |
That’s all about converting Set to array in Java.
标签:Convert,Set,Java,Arrays,set,new,Integer,array 来源: https://www.cnblogs.com/kungfupanda/p/16466032.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。