How can I convert an ArrayList of floats to a primitive float array using java 8 streams?
我正在尝试使用Java 8 Stream API将包含
到目前为止我尝试的是这样的:
1 2 | List<Float> floatList = new ArrayList<Float>(); float[] floatArray = floatList.stream().map(i -> i).toArray(float[]::new) |
老实说:用流来做这个没有好的内置方法。对不起,但这是事实。
如果你可以使用
如果要对流执行此操作,则需要编写自己的自定义大小调整列表类型并收集到该类型,但除此之外没有任何作用。你必须写一些像
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 29 30 31 | float[] toFloatArray(Collection<Float> collection) { class FloatArray { int size; float[] array; FloatArray() { this.size = 0; this.array = new float[10]; } void add(float f) { if (size == array.length) { array = Arrays.copyOf(array, array.length * 2); } array[size++] = f; } FloatArray combine(FloatArray other) { float[] resultArray = new float[array.length + other.array.length]; System.arraycopy(this.array, 0, resultArray, 0, size); System.arraycopy(other.array, 0, resultArray, size, other.size); this.array = resultArray; this.size += other.size; return this; } float[] result() { return Arrays.copyOf(array, size); } } return collection.stream().collect( Collector.of( FloatArray::new, FloatArray::add, FloatArray::combine, FloatArray::result)); } |
将您的集合直接转换为没有流的
1 2 3 4 5 | float[] result = new float[collection.size()]; int i = 0; for (Float f : collection) { result[i++] = f; } |
或者与第三方库,例如番石榴
如果将Eclipse Collections
1 2 3 4 5 6 7 8 9 10 |
您也可以使用
1 2 |
如果使用内置的原始集合,则可以将
1 | float[] array = FloatLists.mutable.with(1.0f, 2.0f, 3.0f).toArray(); |
注意:我是Eclipse Collections的提交者。
这个怎么样?您可以使用FloatBuffer来收集
1 2 3 4 5 6 7 | float[] result = floatList.stream().collect( ()-> FloatBuffer.allocate(floatList.size()), FloatBuffer::put, (left, right) -> { throw new UnsupportedOperationException("only be called in parallel stream"); } ).array(); |
将
可能的替代方式(假设
来自apache的Commons通过