sorting on keys on the MapStream
Arul Dhesiaseelan
aruld at acm.org
Sat Sep 15 12:34:45 PDT 2012
I was trying to rewrite the FunctionalCollectionComputation [1] example
using the updated APIs in latest build [2].
names.stream()
.mapped(name -> name.length())
.filterValues((length) -> length >= 4)
.swap() //optional
.sorted((i1, i2) -> i1 - i2) or sorted((name1, name2) ->
name1.length() - name2.length())
.forEach((name, length) -> System.out.println(name + '\t' +
length));
I had to introduce a swap() on the MapStream so I can sort it on the keys.
Another option would be to use sorted((name1, name2) -> name1.length() -
name2.length()) which won't require a swap().
Down to 4 bulk ops calls from its original version, pretty sweet!
PS:
I understand these APIs are undergoing heavy changes. sorted() throws NPE
in MapPipeline which returns null. so this example won't work under latest
build.
[1] https://gist.github.com/2968389
[2] http://code.google.com/p/openjdk-osx-build/
More information about the lambda-dev
mailing list