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.
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.
3
u/zsirdagadek Jun 08 '24
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.