Lambda, Streams and Collectors Lab
Speakers: Stuart Marks, Maurice Naftalin, Jose Paumard & Gustavo Durand
For more blog posts, see The Oracle Code One table of contents
The lab is self paced
I like that it is organized by topic so you can pick what you want to learn. Since “someone” believed I didn’t need to be here, I decided to blog about what I did and learned.
- I forgot Comparator.naturalOrder() exists because I hardly use it.
- I forgot you can’t use Function.identity() with primitives and instead have to write a lambda: ex: x -> x.
- I almost never use flatMap. I didn’t think to use it combined with splitAsStream to read words from a file. I do a lot of file processing though so this is definitely an idiom I need to remember! I shall type it in for each exercise in this lab that uses it (vs copy/paste) in order to ingrain it in my fingers! reader.lines().flatMap(SPLIT_PATTERN::splitAsStream)
- The extra challenge to write a groupingBy using toMap. I knew I needed to use a merge function, but the types didn’t match my expectations. I learned that:
- if you write a value instead of a lambda for the value function, you get a compiler error on the merge function (about the + operator being invalid ) and not the value function for not being a lambda
- if you write a value function that returns an int instead of a long, you get a compiler error on the merge function and not the value function (because the merge function result is what gets set to the return value)
- If you try to read from a reader that has already called reader.lines(), you don’t get any lines. Doh!
- flatMap(String::chars) doesn’t work because chars() returns an IntStream. flatMapToInt(String::chars) does work
- Entry has comparingByKey() and comparingByValue() methods
- Remember to use groupingBy when aggregating and toMap when one to one
- Need to call boxed() on an IntStream to be able to use partitioning by. An IntStream doesn’t have a collect method that takes a Collector as a parameter
The lab is great. I like that it can be as easy or as hard as you want. I like they support multiple versions of Java and multiple IDEs.
The room is terrible. It’s not really a room. It’s a grid of pipe/drape separated areas. I can hear every word in the room next to us.