1
ArrayList<ArrayList<Float>> lists = new ArrayList<ArrayList<Float>>();

This is the arraylist I'm using. And I tried this:

float[][] trainingData = lists.stream()
                        .map(l -> l.stream()
                       .mapToFloat(Float::parseFloat)
                       .toArray())
                       .toArray(float[][]::new);

I'm getting this error :

The method mapToFloat(Float::parseFloat) is undefined for the type Stream

I must convert this two dimensional ArrayList into two dimensional Float array.

3
  • lists.stream() .flatMap(Collection::stream) .toArray(float[][]::new); Commented May 26, 2020 at 13:45
  • Can you use a Float[][] instead of a float[][]? If not, your only choice is a double for loop. Commented May 26, 2020 at 13:46
  • 1
    This answer contains a collector for a float[] array. Seeing it, you will likely realize that using a loop is simpler… Commented May 26, 2020 at 16:14

3 Answers 3

2

First and foremost the reason why your code doesn't work is because there's no such method mapToFloat in the Java API but most importantly there is no FloatStream.

your options are quite limited if you want to accomplish this via streams:

  1. accumulate to a Float[][] instead of a float[][], which might not be ideal as it involves boxing and unboxing bottleneck when you want to perform some computation later on in your application.

this can be accomplished via:

Float[][] trainingData = lists.stream()
                .map(l -> l.toArray(new Float[0]))
                .toArray(Float[][]::new);
  1. use a List<List<Double>> in the first place rather than a List<List<Float>>to represent your training data, then you can just convert it to a double[][] when required:

this can be accomplished via:

double[][] trainingData = lists.stream()
                .map(l -> l.stream().mapToDouble(Double::doubleValue).toArray())
                .toArray(double[][]::new);

but if you cannot change the initial type of data for whatever reasons then you can still covert to a double[][].

this can be accomplished via:

double[][] trainingData = lists.stream()
                .map(l -> l.stream().mapToDouble(Float::doubleValue).toArray())
                .toArray(double[][]::new);
  1. define your own Collector which you should be able to find within the site with some research.

Ultimately you're probably better off using an imperative approach here i.e. for loops.

Sign up to request clarification or add additional context in comments.

3 Comments

When you say “use a List<List<Double>> in the first place”, it’s contradicting to use Float::doubleValue in the example code.
@Holger I see where you're coming from but that piece of code was referring to but if you cannot change the initial type of data for whatever reasons then you can still covert to a double[][].. i'll try to clarify that.
So there is no example for converting List<List<Double>> to double[][], actually. But when you change Float::doubleValue to Number::doubleValue, the code works regardless of whether the OP uses List<List<Double>> or List<List<Float>>
1

Try this:

Float[][] trainingData = lists
             .stream().map(lst -> lst.stream().toArray(Float[]::new))
             .toArray(Float[][]::new);

If you need primitive arrays, the only way I can think of is what you probably already knew (but I include for completeness).

float[][] trainingData = new float[lists.size()][];

for (int i = 0; i < lists.size(); i++) {
    List<Float> innerList = lists.get(i);
    float[] temp = trainingData[i] = new float[innerList.size()];
    for (int k = 0; k < temp.length; k++) {
            temp[k] = innerList.get(k);
    }
}

for(float[] v : trainingData) {
    System.out.println(Arrays.toString(v));
}

Both print

[10.0, 10.2]
[20.4, 2.6]

Comments

0

For float there is no good way with stream, you can get as 2D double array this way

double[][] trainingData = lists.stream()
        .map(l -> l.stream().mapToDouble(f -> f.doubleValue()).toArray()).toArray(double[][]::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.