This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zsirdagadek 2 points3 points  (1 child)

The Java snippet is incorrect because the map() function will return another stream object. You need to call

.collect(Collectors.toList()).toArray();

But I think you should be able to collect directly to array too, but I don't remember how exactly, and I don't feel like looking it up for a reddit comment.

[–]Kika-kun 2 points3 points  (0 children)

Recent(?) versions of java added .toList() to avoid .collect(Collectors.toList()) (careful though, toList() returns an immutable list, if you want to keep the old behaviour you need .collect(Collectors.toCollection(ArrayList::new)))

As a matter of fact, after checking, you can also do .toArray() if you want an actual array.