This is nearly the same question as this one, but in the opposite direction.
I know there's no FloatStream
in Java 8, and there're no many use cases for float[], but I have one:
Dealing with TriangleMesh
in JavaFX 3D, you have to provide an ObservableFloatArray
for the 3D coordinates of the vertices of the whole mesh.
As a result of some calculations I'll have all these coordinates in a List
, and to add all of them at once to the mesh, I'll call triangleMesh.getPoints().addAll()
, using one of the following methods:
- addAll(ObservableFloatArray src)
- addAll(float... elements)
- addAll(ObservableFloatArray src, int srcIndex, int length)
- addAll(float[] src, int srcIndex, int length)
where ObservableFloatArray
can be created using FXCollections.observableFloatArray()
, FXCollections.observableFloatArray(ObservableFloatArray array)
or FXCollections.observableFloatArray(float... values)
.
Let's say I have this pojo for each vertex:
private class Vertex {
private final float x;
private final float y;
private final float z;
public Vertex(float x, float y, float z){
this.x=x; this.y=y; this.z=z;
}
public float[] getCoordenates(){
return new float[]{x,y,z};
}
}
and after performing some calculations I have List<Vertex> listVertices
. I'll need to generate float[] arrayVertices
to finally call triangleMesh.getPoints().addAll(arrayVertices);
.
For now this is what I'm doing:
listVertices.forEach(vertex->triangleMesh.getPoints().addAll(vertex.getCoordenates()));
But this triggers the associated listener on every new vertex added to the observable array, and for high number of vertices this affects performance.
Should FloatStream
and flatMapToFloat()
exist, I'd do something like this:
float[] arrayVertices = listVertices.stream()
.map(vertex->FloatStream.of(vertex.getCoordenates()))
.flatMapToFloat(f->f).toArray();
triangleMesh.getPoints().addAll(arrayVertices);
like I actually do with a list of int[] for face indices:
int[] arrayFaces = listFaces.stream()
.map(face->IntStream.of(face.getFaceIndices()))
.flatMapToInt(i->i).toArray();
triangleMesh.getFaces().addAll(arrayFaces);
But as far as I know, there's no way using streams.
Thanks in advance for any possible solution involving streams.