Class Work
Collect Stream result
- Collecting stream result is terminal operation.
- Object[] toArrray()
- R collect(Collector)
- Collectors.toList(), Collectors.toSet(), Collectors.toCollection(), Collectors.joining()
- Collectors.toMap(key, value)
Stream of primitive types
- Efficient in terms of storage and processing. No auto-boxing and unboxing is done.
- IntStream class
- IntStream.of() or IntStream.range() or IntStream.rangeClosed() or Random.ints()
- sum(), min(), max(), average(), summaryStatistics()
Method references
- If lambda expression involves single method call, it can be shortened by using method reference.
- Method references are converted into instances of functional interfaces.
- Method reference can be used for class static method, class non-static method, object non-static method or constructor.
Examples
- Class static method: Integer::sum [ (a,b) -> Integer.sum(a,b) ]
- Both lambda param passed to static function explicitly
- Class non-static method: String::compareTo [ (a,b) -> a.compareTo(b) ]
- First lambda param become implicit param (this) of the function and second is passed explicitly (as arguments).
- Object non-static method: System.out::println [ x -> System.out.println(x) ]
- Lambda param is passed to function explicitly.
- Constructor: Date::new [ () -> new Date() ]
- Lambda param is passed to constructor explicitly.
enum
.png)
.png)