13

I’m trying to convert a map based Stream into a two-dimensional array. I have figured out how to store it in a one dimensional array. Here is working code snippet:

Float[] floatArray = map.entrySet()
                        .stream()
                        .map(key -> key.getKey().getPrice())
                        .toArray(size -> new Float[size]);

When I execute the above code, I get my Float array populated as expected. Now I need to extend this to a two-dimensional array where I need to store the result in first dimension of a 2d array along these lines:

Float[][1] floatArray = map.entrySet()
                           .stream()
                           .map(key -> key.getKey().getPrice())
                           .toArray(size -> new Float[size][1]);

The code above does not work. Can you please let me know how to accomplish this task with Java 8 streams? Thanks in advance!

1
  • 1
    "convert and map based stream to a two-dimensional array" - please post a (small) example of such map and how is the 2D array supposed to look like (expected output). Commented Nov 20, 2017 at 17:17

2 Answers 2

12

If you look at <A> A[] toArray(IntFunction<A[]> generator), you see that it converts a Stream<A> to a A[], which is a 1D array of A elements. So in order for it to create a 2D array, the elements of the Stream must be arrays.

Therefore you can create a 2D array if you first map the elements of your Stream to a 1D array and then call toArray:

Float[][] floatArray = 
    map.entrySet()
       .stream()
       .map(key -> new Float[]{key.getKey().getPrice()})
       .toArray(size -> new Float[size][1]);
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much, that works! My next question is if I want to populate three dimensions of my floatArray from the map for instance: Float[][] floatArray = map.entrySet() .stream() .map(key -> new Float[]{key.getKey().getPrice()},{key.getKey().getOpen()},{key.getKey().getClose()}) .toArray(size -> new Float[size][3]); How can this be accomplished?
I want to be able to first populate all three dimensions/columns of my 2D floatArray from map entries and then loop through the floatArray later in the code to perform downstream calculations using floatArray like so: for (int i=0; i<floatArray.rows; i++) { for (int j=0; j<floatArray.cols; j++) { //do some calcs here someCalc = floatArray[i][j] * 2; }
One last request, in the third column of floatArray, I want to store the counter for example if there are 10 entries in the map, I want to store value 1...10 in third column. Thanks
I was able to figure out how to populate multiple dimensions: Float[][] floatArray = map.entrySet() .stream() .map(key -> new Float[]{(float) 0.00,key.getKey().getPrice(),(float) 0.00}) .toArray(size -> new Float[size][3]); The only thing left is to add a counter in the first position or how to replace (float) 0.00 in position 1 with a counter. Thanks
@MazS that's still a 2D array, not 3D. If that's what you wanted, your comment about "three dimensions" was confusing.
|
8

You can make use of the following:

Float[][] array = map.entrySet()
    .stream()
    .map(Map.Entry::getKey)
    .map(YourKeyClass::getPrice) // 1)
    .map(price -> new Float[]{ price })
    .toArray(Float[][]::new);

Which creates a 2D array just like you expected.

Note: By the comment 1) you have to replace YourKeyClass with the class containing the method getPrice() returning a Float object.


An alternative is also to use .keySet() instead of .entrySet():

Float[][] array = map.keySet().stream()
    .map(YourKeyClass::getPrice)
    .map(price -> new Float[]{price})
    .toArray(Float[][]::new);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.