Java 8
-
Upload
fasihul-kabir -
Category
Software
-
view
87 -
download
0
Transcript of Java 8
![Page 1: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/1.jpg)
Lambda, Method References & Aggregate Operations
Md. Fasihul KabirSoftware Engineer, Platform Dhaka, Escenic Asia Ltd.
![Page 2: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/2.jpg)
Some Cool Features of Java 8� Lambda expressions� Method references� Default Methods (Defender methods)� A new Stream API� Optional� A new Date/Time API� Nashorn, the new JavaScript engine� Removal of the Permanent Generation� and more…
![Page 3: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/3.jpg)
Some Cool Features of Java 8� Lambda Expressions� Method References� Default Methods (Defender methods)� A new Stream API� Optional� A new Date/Time API� Nashorn, the new JavaScript engine� Removal of the Permanent Generation� and more…
![Page 4: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/4.jpg)
Lambda Expressions
![Page 5: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/5.jpg)
Syntax
(arguments) -> {body}
![Page 6: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/6.jpg)
Example
button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){
System.out.println("button clicked");}
});
![Page 7: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/7.jpg)
button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){
System.out.println("button clicked");}
});
![Page 8: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/8.jpg)
button.addActionListener(event -> System.out.println("button clicked")
);
![Page 9: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/9.jpg)
Structure of Lambda Expressions� A lambda expression can have zero, one or more parameters� The type of the parameters can be explicitly declared or it can be inferred from the
context. e.g. (int a) is same as just (a)
� Parameters are enclosed in parentheses and separated by commas. e.g. (a, b) or (int a, int b) or (String a, int b, float c)
� Empty parentheses are used to represent an empty set of parameters. e.g. () -> 42
� When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses
e.g. a -> return a*a� The body of the lambda expressions can contain zero, one or more statements� If body of lambda expression has single statement curly brackets are not mandatory and
the return type of the anonymous function is the same as that of the body expression� When there is more than one statement in body than these must be enclosed in curly
brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned
![Page 10: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/10.jpg)
Method References
![Page 11: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/11.jpg)
![Page 12: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/12.jpg)
![Page 13: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/13.jpg)
![Page 14: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/14.jpg)
![Page 15: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/15.jpg)
![Page 16: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/16.jpg)
Kinds of Method ReferencesKind ExampleReference to a static method ContainingClass::staticMethodNameReference to an instance method of a particular object
containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type
ContainingType::methodName
Reference to a constructor ClassName::new
![Page 17: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/17.jpg)
Reference to a Static Method� The method reference Person::compareByAge is a
reference to a static method.
![Page 18: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/18.jpg)
Reference to an Instance Method of a Particular Object
![Page 19: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/19.jpg)
Reference to an Instance Method of an Arbitrary Object of a Particular Type
![Page 20: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/20.jpg)
Reference to a Constructor
![Page 21: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/21.jpg)
![Page 22: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/22.jpg)
![Page 23: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/23.jpg)
Aggregate Operations
![Page 24: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/24.jpg)
![Page 25: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/25.jpg)
![Page 26: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/26.jpg)
![Page 27: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/27.jpg)
Pipelines and Streams
![Page 28: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/28.jpg)
Components of Pipeline� A source: This could be a collection, an array, a
generator function, or an I/O channel. In this example, the source is the collection roster.
� Zero or more intermediate operations. An intermediate operation, such as filter, produces a new stream.
� A terminal operation. A terminal operation, such as forEach, produces a non-stream result, such as a primitive value (like a double value), a collection, or in the case of forEach, no value at all.
![Page 29: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/29.jpg)
![Page 30: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/30.jpg)
![Page 31: Java 8](https://reader030.fdocuments.net/reader030/viewer/2022032422/55a8b7e81a28ab20628b48d9/html5/thumbnails/31.jpg)