Updated Java 8 Stream examples in GS Collections Kata
Raab, Donald
Donald.Raab at gs.com
Mon Nov 12 21:04:55 PST 2012
I updated the Java 8 Lambda version of our GS Collections Kata. I used the latest version of the Java 8 Lambda binary (November).
Updated tests are here:
https://github.com/goldmansachs/gs-collections-kata/tree/solutions-java8-jcf/src/test/java/com/gs/collections/kata
Updated domain is here:
https://github.com/goldmansachs/gs-collections-kata/tree/solutions-java8-jcf/src/main/java/com/gs/collections/kata
The things I found awkward using in the kata were flatMap and Optional in the null case. Examples of both are as follows in the Company domain class:
https://github.com/goldmansachs/gs-collections-kata/blob/solutions-java8-jcf/src/main/java/com/gs/collections/kata/Company.java
Compare with GS Collections flatCollect and detect in this example.
https://github.com/goldmansachs/gs-collections-kata/blob/solutions-java8/src/main/java/com/gs/collections/kata/Company.java
You'll note the need to cast null to Customer in the orElse case due to the overloaded method taking a Factory.
I found flatMap unintuitive and I now understand the confusion it was causing. Here are two more examples in Exercise 4 and Exercise 9. Compare the findItemNames (Ex4) mostExpensiveItem (Ex9) tests between Java 8 Streams and GS Collections with Lambdas.
Java 8 flatMap
https://github.com/goldmansachs/gs-collections-kata/blob/solutions-java8-jcf/src/test/java/com/gs/collections/kata/Exercise4Test.java
https://github.com/goldmansachs/gs-collections-kata/blob/solutions-java8-jcf/src/test/java/com/gs/collections/kata/Exercise9Test.java
GS Collections flatCollect
https://github.com/goldmansachs/gs-collections-kata/blob/solutions-java8/src/test/java/com/gs/collections/kata/Exercise4Test.java
https://github.com/goldmansachs/gs-collections-kata/blob/solutions-java8/src/test/java/com/gs/collections/kata/Exercise9Test.java
It would be much nicer to have a form of flatMap that allows for using a method reference IMO. This will be a very common use case, especially in cases where there is an object graph with one-to-many relationships.
The Goldman Sachs Group, Inc. All rights reserved.
See http://www.gs.com/disclaimer/global_email for important risk disclosures, conflicts of interest and other terms and conditions relating to this e-mail and your reliance on information contained in it. This message may contain confidential or privileged information. If you are not the intended recipient, please advise us immediately and delete this message. See http://www.gs.com/disclaimer/email for further information on confidentiality and the risks of non-secure electronic communication. If you cannot access these links, please notify us by reply message and we will send the contents to you.
More information about the lambda-libs-spec-observers
mailing list