Java Magazine, Mar/Apr 2018
ORACLE COM JAVAMAGAZINE MARCH APRIL 2018 64 jvm languages assert nums stream mapToInt n n 2 Expression lambda filter n Block lambda return n 3 0 sum 36 Note the use of the expression lambda syntax which does not require either braces or a return statement in the mapToInt intermediate method on stream Just to show the alternative a block lambda was used in the filter method You can also use Java method references Groovy uses the ampersand operator as a way to refer to methods but Java uses a double colon For example one of the additions to the API in Java 8 was the static sum method in the Integer class which can be used as a BinaryOperator argument to the reduce method The following code is written in Groovy but uses the standard Java syntax assert nums stream mapToInt n n 2 filter n n 3 0 reduce 0 Integer sum 36 BinaryOperator The point is that all the Java syntax works with the new Groovy parser Groovy adds the ability to add default parameter values to lambdas too Groovy goes well beyond what Java provides For example Groovy has AST transformations that generate useful code at compile time Consider the classic Fibonacci calculation which is easy to do inefficiently @ Memoized long fibonacci long n if n 2 1
You must have JavaScript enabled to view digital editions.