Download - Java 8 presentation
![Page 1: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/1.jpg)
JAVA 8At First Glance
VISION Team - 2015
![Page 2: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/2.jpg)
Agenda
❖ New Features in Java language ➢ Lambda Expression➢ Functional Interface➢ Interface’s default and Static Methods➢ Method References
❖ New Features in Java libraries➢ Stream API➢ Date/Time API
![Page 3: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/3.jpg)
Lambda Expression
What is Lambda Expression?
![Page 4: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/4.jpg)
❖Unnamed block of code (or an unnamed function) with a list of formal parameters and a body.✓ Concise✓ Anonymous✓ Function✓ Passed around
Lambda Expression
![Page 5: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/5.jpg)
Lambda Expression
Why should we care about Lambda Expression?
![Page 6: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/6.jpg)
Example 1:Comparator<Person> byAge = new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.getAge().compareTo(p2.getAge()); }};
Lambda Expression
Example 2:JButton testButton = new JButton("Test Button");testButton.addActionListener(new ActionListener(){@Override public void actionPerformed(ActionEvent ae){ System.out.println(“Hello Anonymous inner class"); }});
![Page 7: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/7.jpg)
Example 1: with lambdaComparator<Person> byAge = (Person p1, Person p2) -> p1.getAge().compareTo(p2.getAge());
Lambda Expression
Example 2: with lambdatestButton.addActionListener(e -> System.out.println(“Hello Lambda"));
![Page 8: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/8.jpg)
Lambda Expression
Lambda Syntax
![Page 9: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/9.jpg)
(Person p1, Person p2) -> p1.getAge().compareTo(p2.getAge());
Lambda Expression
Lambda parameters Lambda body
Arrow
The basic syntax of a lambda is either(parameters) -> expression
or(parameters) -> { statements; }
![Page 10: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/10.jpg)
❖ Lambda does not have:✓ Name ✓ Return type✓ Throws clause✓ Type parameters
Lambda Expression
![Page 11: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/11.jpg)
Examples:1. (String s) -> s.length()2. (Person p) -> p.getAge() > 203. () -> 924. (int x, int y) -> x + y5. (int x, int y) -> {
System.out.println(“Result:”);System.out.println(x + y);
}
Lambda Expression
![Page 12: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/12.jpg)
Variable scope:public void printNumber(int x) {
int y = 2;Runnable r = () -> System.out.println(“Total is:” + (x+y));new Thread(r).start();}
Lambda Expression
Free variable: not define in lambda parameters.
Variable scope:public void printNumber(int x) {
int y = 2;Runnable r = () ->{System.out.println(“Total is:” + (x+y));
System.out.println(this.toString());};new Thread(r).start();}
Not allow:public void printNumber(int x) {
int y = 2;Runnable r = () -> {
x++;//compile errorSystem.out.println(“Total is:” + (x+y))
};}
![Page 13: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/13.jpg)
Lambda Expression
Where should we use Lambda?
![Page 14: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/14.jpg)
Comparator:Comparator<Person> byAge = (Person p1, Person p2) -> p1.getAge().compareTo(p2.getAge());Collections.sort(personList, byAge);
Lambda Expression
Listener:ActionListener listener = e -> System.out.println(“Hello Lambda")testButton.addActionListener(listener );
Example:public void main(String[] args){(int x, int y) -> System.out.println(x + y);}
![Page 15: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/15.jpg)
Runnable:// Anonymous classRunnable r1 = new Runnable(){ @Override public void run(){ System.out.println("Hello world one!"); }};// Lambda RunnableRunnable r2 = () -> System.out.println("Hello world two!");
Lambda Expression
![Page 16: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/16.jpg)
Iterator:
List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");//Prior to Java 8 :for (String feature : features) { System.out.println(feature);} //In Java 8:Consumer<String> con = n -> System.out.println(n)features.forEach(con);
Lambda Expression
![Page 17: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/17.jpg)
Demonstration
![Page 18: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/18.jpg)
What is functional interface?
Add text here...Functional Interface
![Page 19: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/19.jpg)
● New term of Java 8● A functional interface is an interface with
only one abstract method.
Add text here...Functional Interface
![Page 20: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/20.jpg)
● Methods from the Object class don’t count.
Add text here...Functional Interface
![Page 21: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/21.jpg)
Annotation in Functional Interface
Add text here...Functional Interface
![Page 22: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/22.jpg)
● A functional interface can be annotated.● It’s optional.
Add text here...Functional Interface
![Page 23: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/23.jpg)
● Show compile error when define more than one abstract method.
Add text here...Functional Interface
![Page 24: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/24.jpg)
Functional Interfaces Toolbox
Add text here...Functional Interface
![Page 25: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/25.jpg)
● Brand new java.util.function package.● Rich set of function interfaces.● 4 categories:o Suppliero Consumero Predicateo Function
Add text here...Functional Interface
![Page 26: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/26.jpg)
● Accept an object and return nothing.
● Can be implemented by lambda expression.
Add text here...Consumer Interface
![Page 27: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/27.jpg)
Demonstration
![Page 28: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/28.jpg)
Interface Default and Static Methods
Add text here...Interface Default and Static Methods
![Page 29: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/29.jpg)
static <T> Collection<T> synchronizedCollection(Collection<T> c)
Interface Default and Static Methods
● Advantages: - No longer need to provide your own companion utility classes. Instead, you
can place static methods in the appropriate interfaces
- Adding methods to an interface without breaking the existing implementation
java.util.Collections
java.util.Collection
● Extends interface declarations with two new concepts:- Default methods- Static methods
![Page 30: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/30.jpg)
Interface Default and Static Methods
[modifier] default | static returnType nameOfMethod (Parameter List) {
// method body }
● Syntax
![Page 31: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/31.jpg)
Default methods● Classes implement interface that contains a default method
❏ Not override the default method and will inherit the default method❏ Override the default method similar to other methods we override in
subclass❏ Redeclare default method as abstract, which force subclass to override it
![Page 32: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/32.jpg)
Default methods● Solve the conflict when the same method declare in interface or
class
- Method of Superclasses, if a superclass provides a concrete method.
- If a superinterface provides a default method, and another interface supplies a method with the same name and parameter types (default or not), then you must overriding that method.
![Page 33: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/33.jpg)
Static methods● Similar to default methods except that we can’t override
them in the implementation classes
![Page 34: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/34.jpg)
Demonstration
![Page 35: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/35.jpg)
Method References● Method reference is an important feature related to
lambda expressions. In order to that a method reference requires a target type context that consists of a compatible functional interface
![Page 36: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/36.jpg)
Method References● There are four kinds of method references:
- Reference to a static method- Reference to an instance method of a particular object- Reference to an instance method of an arbitrary object
of a particular type- Reference to a constructor
![Page 37: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/37.jpg)
Method References● Reference to a static methodThe syntax: ContainingClass::staticMethodName
![Page 38: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/38.jpg)
Method References● Reference to an instance method of a particular object
The syntax: containingObject::instanceMethodName
![Page 39: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/39.jpg)
Method References● Reference to an instance method of an arbitrary object of a
particular typeThe syntax: ContainingType::methodName
![Page 40: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/40.jpg)
Method References● Reference to a constructorThe syntax: ClassName::new
![Page 41: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/41.jpg)
Method References● Method references as Lambdas Expressions
Syntax Example As LambdaClassName::new String::new () -> new String()
Class::staticMethodName String::valueOf (s) -> String.valueOf(s)
object::instanceMethodName x::toString () -> "hello".toString()
Class::instanceMethodName String::toString (s) -> s.toString()
![Page 42: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/42.jpg)
What is a Stream?
Add text here...Stream
![Page 43: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/43.jpg)
Old Java:
Add text here...Stream
![Page 44: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/44.jpg)
Add text here...StreamJava 8:
![Page 45: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/45.jpg)
❏Not store data❏Designed for processing data❏Not reusable❏Can easily be outputted as arrays or
lists
Add text here...Stream
![Page 46: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/46.jpg)
1. Build a stream
Add text here...Stream - How to use
2. Transform stream3. Collect result
![Page 47: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/47.jpg)
Build streams from collectionsList<Dish> dishes = new ArrayList<Dish>();....(add some Dishes)....Stream<Dish> stream = dishes.stream();
Add text here...Stream - build streams
![Page 48: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/48.jpg)
Build streams from arraysInteger[] integers =
{1, 2, 3, 4, 5, 6, 7, 8, 9};Stream<Integer> stream = Stream.of(integers);
Add text here...Stream - build streams
![Page 49: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/49.jpg)
Intermediate operations (return Stream)❖ filter()
❖map()
❖ sorted()
❖….
Add text here...Stream - Operations
![Page 50: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/50.jpg)
// get even numbersStream<Integer> evenNumbers = stream
.filter(i -> i%2 == 0);
// Now evenNumbers have {2, 4, 6, 8}
Add text here...Stream - filter()
![Page 51: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/51.jpg)
Terminal operations❖ forEach()
❖ collect()
❖match()
❖….
Add text here...Stream - Operations
![Page 52: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/52.jpg)
// get even numbersevenNumbers.forEach(i -> System.out.println(i));
/* Console output * 2 * 4 * 6 * 8 */
Add text here...Stream - forEach()
![Page 53: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/53.jpg)
Converting stream to listList<Integer> numbers =
evenNumbers.collect(Collectors.toList());
Converting stream to arrayInteger[] numbers =
evenNumbers.toArray(Integer[]::new);
Add text here...Stream - Converting to collections
![Page 54: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/54.jpg)
Demonstration
![Page 55: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/55.jpg)
What’s new in Date/Time?
Add text here...Date / Time
![Page 56: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/56.jpg)
● Why do we need a new Date/Time API?
Add text here...Date / Time
○ Objects are not immutable○ Naming○ Months are zero-based
![Page 57: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/57.jpg)
● LocalDate
Add text here...Date / Time
○ A LocalDate is a date, with a year, month, and day of the month
LocalDate today = LocalDate.now();
LocalDate christmas2015 = LocalDate.of(2015, 12, 25);
LocalDate christmas2015 = LocalDate.of(2015, Month.DECEMBER, 25);
![Page 58: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/58.jpg)
Java 7:
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date dt = c.getTime();
Java 8:
LocalDate tomorrow = LocalDate.now().plusDay(1);
Add text here...Date / Time
![Page 59: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/59.jpg)
● Temporal adjuster
Add text here...Date / Time
LocalDate nextPayDay = LocalDate.now() .with(TemporalAdjusters.lastDayOfMonth());
![Page 60: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/60.jpg)
● Create your own adjuster
TemporalAdjuster NEXT_WORKDAY = w -> {
LocalDate result = (LocalDate) w; do {
result = result.plusDays(1); } while (result.getDayOfWeek().getValue() >= 6); return result;
};
LocalDate backToWork = today.with(NEXT_WORKDAY);
Add text here...Date / Time
![Page 62: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/62.jpg)
Add text here...References❖ Java Platform Standard Edition 8 Documentation, (
http://docs.oracle.com/javase/8/docs/)❖ Java 8 In Action, Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft.❖ Beginning Java 8 Language Features, Kishori Sharan.❖ What’s new in Java 8 - Pluralsight
![Page 63: Java 8 presentation](https://reader035.vdocuments.site/reader035/viewer/2022081507/5872e65d1a28abfa548b6163/html5/thumbnails/63.jpg)
Add text here...The End