SlideShare a Scribd company logo
New Feature in Java 8
Người trình bày: Anh Vương
Hà Nội ngày 24 tháng 07 năm 2018
Functional Interface
Java Stream
Optional
Java Time API
Một số tính năng khác
1. Functional Interface
Là interface có duy nhất một method abstract
Sử dụng Anotation @FunctionalInterface
Benefit: Sử dụng được biểu thức lambda Expression
Ex:
@FunctionalInterface
public interface FunctionalDemo<T> {
String field = "";
String abstractMethod();
}
1. Functional Interface
Lambda Expression called anonymous function, a function (or a
subroutine) defined, and possibly called, without being bound to an
identifier
Syntax: (arguments) -> (body)
Ex:
() -> System.out.println("Zero parameter lambda");
(param) -> System.out.println("One parameter: " + param);
param -> System.out.println("One parameter: " + param);
(p1,p2)-> System.out.println(“Parameter 1:” + p1+”, parameter 2”+p2);
(Car car) -> System.out.println("The car is: " + car.getName());
(oldState, newState) -> {
System.out.println("Old state: " + oldState);
System.out.println("New state: " + newState);
}
(param) -> System.out::println;
1. Functional Interface
Benefit của lambda Expression
• Giảm số lượng dòng code
• Truyền function như một arguments
Link tham khảo Lambda Expression trong Java 8
http://tutorials.jenkov.com/java/lambda-expressions.html
1. Functional Interface
Bổ sung method default trong interface
Why?
- Thay đổi cấu trúc interface mà không muốn thay đổi các implement của nó
- Mở rộng theo hướng tương thích ngược
Note:
Default method trong multiple inheritance
Phân biệt abstract class và interface
2. Java Stream
Why?
- Giảm code khi sử dụng kết hợp với lambda Expression
- Tăng hiệu năng khi sử internal iteration (có thể execute tuần tự hoặc song song)
What?
- Java Stream là một cấu trúc dữ liệu được tính toán theo yêu cầu
- Java Stream không lưu data, nó hoạt động trên cấu trúc dữ liệu nguồn,
tạo ra các pipeline data
2. Java Stream
Đặc điểm:
- Internal iteration principle sẽ giúp lazy-seeking trong một số operations
của stream
Ex: Filter, map,.. có thể implemented lazily cho phép hiệu năng cao hơn
- Java Stream không lưu data, nó là tạo ra các pipeline data nên khi sử
dụng (thực hiện các operation filter, map) nó sẽ lập tức thay đổi mà không
thể sử dụng lại
- Java stream hỗ trợ xử lí tuần tự và song song (sequential và parallel)
Một số Functional Method sử dụng trong Java 8 Stream:
- Function và BiFunction
- Predicate và BiPredicate
- Consumer và BiConsumer
- Supplier
2. Java Stream
Java Stream Intermediate and Terminal Operations
- Operations của Java 8 Stream API return new Stream thì được gọi
là các intermediate Operations
Thường trả về các kết quả trung gian
Một số Operations thường sử dụng: filter, map, limit, skip
- Ngược lại các operation của Java 8 Stream return một kết quả khác
thì được gọi là Terminal Operations
Một số Operations: forEach, toArray, min, max, findFirst,
anyMatch, allMatch
2. Java Stream
Short Circuiting
- Các Operations tạo ra các stream hữu hạn (finite stream) cho một
stream vô hạn (infinite stream) được gọi là short circuiting.
Ex: limit, skip
- Các Operations may terminate in finite time for infinite stream.
Ex: anyMatch, allMatch, noneMatch, findFirst, findAny
3. Optional
- Là một kiểu dữ liệu mới được Java 8 giới thiệu
- Là một container object use to contain not-null objects
- Optional có nhiều tiện ích để tạo điều kiện cho việc xử lý các giá trị dưới
dạng có sẵn hoặc không thay vì việc phải check null
4. Java Time API
Why?
- Các kiểu data không nhất quán
- Các classes Date đều là mutable và không thread safe
Đặc điểm:
- Tất cả các classes trong new Date Time API đều là inmutable, thread safe
- Tách biệt giữa các khái niệm: Date, Time, DateTime, Timestamp, TimeZone
- Các method định nghĩa rõ rang
- Có thêm các tiện ích cho việc tính toán thời gian
- Có thể dễ dàng mở rộng
4. Java Time API
Các package trong Java Time API
- java.time: LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration
- java.time.chrono: Định nghĩa các class không theo chuẩn ISO, có thể extend để
tạo hệ thống calendar riêng
- java.time.format: Định nghĩa các class sử dụng để formating và parsing
- java.time.temporal: Giúp tìm kiếm một số ngày đặc biệt (đầu tháng, cuối
tháng,…)
- java.time.zone: Hỗ trợ về time zone
5. Một số tính năng khác
Collection API improvements
Concurrency API improvements
Java IO improvements
ThreadLocal bổ sung thêm method withInitial(Supplier supplier)
Comparator
…
Tham khảo: https://www.journaldev.com/2389/java-8-features-with-examples
Referent
https://www.journaldev.com/2389/java-8-features-with-examples
https://www.journaldev.com/2763/java-8-functional-interfaces
https://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant
http://tutorials.jenkov.com/java/lambda-expressions.html
https://en.wikipedia.org/wiki/Lambda_calculus#Definition
https://www.journaldev.com/2774/java-8-stream
https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

More Related Content

DOC
Core java 2
 
DOC
Core java 3
 
PDF
Web201 slide 3
DOC
Core java 8
 
DOC
Core java 7
 
DOC
Core java 9
 
PDF
Oop unit 09 lập trình tổng quát
DOC
T+¦¦ëng h¦í¦úp uml
Core java 2
 
Core java 3
 
Web201 slide 3
Core java 8
 
Core java 7
 
Core java 9
 
Oop unit 09 lập trình tổng quát
T+¦¦ëng h¦í¦úp uml

Similar to Java8 (20)

PPTX
Sơ lược về Java
PDF
Bai08 lap trinhtongquat
PDF
Domain Driven Design và Event Driven Architecture
PPT
LTJAVA_TV_Slides.ppt
PDF
Bai01 oop overview
PPT
BáO CáO Lý ThuyếT Java
PPT
Java Tieng Viet
DOC
Core java 4
 
PDF
PPT
Ky thuat l.trinh_java
DOCX
Giao Diện (Interface) trong Java | Kiến Thức Cơ Bản
PDF
[Cntt] bài giảng java khtn hcm
PPT
On thitotnghiep
PDF
[Cntt] bài giảng lập trình java bkhcm
PDF
Lập Trình Hướng Đối Tượng trong Java ( Vietnamese )
PDF
[Cntt] all java
PPT
bài giảng lập trình java - huỳnh công pháp
PDF
02 - Gioi thieu ngon ngu lap trinh Java (tt).pdf
PDF
Print_to_OOP.pdf
PDF
Sơ lược về Java
Bai08 lap trinhtongquat
Domain Driven Design và Event Driven Architecture
LTJAVA_TV_Slides.ppt
Bai01 oop overview
BáO CáO Lý ThuyếT Java
Java Tieng Viet
Core java 4
 
Ky thuat l.trinh_java
Giao Diện (Interface) trong Java | Kiến Thức Cơ Bản
[Cntt] bài giảng java khtn hcm
On thitotnghiep
[Cntt] bài giảng lập trình java bkhcm
Lập Trình Hướng Đối Tượng trong Java ( Vietnamese )
[Cntt] all java
bài giảng lập trình java - huỳnh công pháp
02 - Gioi thieu ngon ngu lap trinh Java (tt).pdf
Print_to_OOP.pdf
Ad

Java8

  • 1. New Feature in Java 8 Người trình bày: Anh Vương Hà Nội ngày 24 tháng 07 năm 2018
  • 2. Functional Interface Java Stream Optional Java Time API Một số tính năng khác
  • 3. 1. Functional Interface Là interface có duy nhất một method abstract Sử dụng Anotation @FunctionalInterface Benefit: Sử dụng được biểu thức lambda Expression Ex: @FunctionalInterface public interface FunctionalDemo<T> { String field = ""; String abstractMethod(); }
  • 4. 1. Functional Interface Lambda Expression called anonymous function, a function (or a subroutine) defined, and possibly called, without being bound to an identifier Syntax: (arguments) -> (body) Ex: () -> System.out.println("Zero parameter lambda"); (param) -> System.out.println("One parameter: " + param); param -> System.out.println("One parameter: " + param); (p1,p2)-> System.out.println(“Parameter 1:” + p1+”, parameter 2”+p2); (Car car) -> System.out.println("The car is: " + car.getName()); (oldState, newState) -> { System.out.println("Old state: " + oldState); System.out.println("New state: " + newState); } (param) -> System.out::println;
  • 5. 1. Functional Interface Benefit của lambda Expression • Giảm số lượng dòng code • Truyền function như một arguments Link tham khảo Lambda Expression trong Java 8 http://tutorials.jenkov.com/java/lambda-expressions.html
  • 6. 1. Functional Interface Bổ sung method default trong interface Why? - Thay đổi cấu trúc interface mà không muốn thay đổi các implement của nó - Mở rộng theo hướng tương thích ngược Note: Default method trong multiple inheritance Phân biệt abstract class và interface
  • 7. 2. Java Stream Why? - Giảm code khi sử dụng kết hợp với lambda Expression - Tăng hiệu năng khi sử internal iteration (có thể execute tuần tự hoặc song song) What? - Java Stream là một cấu trúc dữ liệu được tính toán theo yêu cầu - Java Stream không lưu data, nó hoạt động trên cấu trúc dữ liệu nguồn, tạo ra các pipeline data
  • 8. 2. Java Stream Đặc điểm: - Internal iteration principle sẽ giúp lazy-seeking trong một số operations của stream Ex: Filter, map,.. có thể implemented lazily cho phép hiệu năng cao hơn - Java Stream không lưu data, nó là tạo ra các pipeline data nên khi sử dụng (thực hiện các operation filter, map) nó sẽ lập tức thay đổi mà không thể sử dụng lại - Java stream hỗ trợ xử lí tuần tự và song song (sequential và parallel) Một số Functional Method sử dụng trong Java 8 Stream: - Function và BiFunction - Predicate và BiPredicate - Consumer và BiConsumer - Supplier
  • 9. 2. Java Stream Java Stream Intermediate and Terminal Operations - Operations của Java 8 Stream API return new Stream thì được gọi là các intermediate Operations Thường trả về các kết quả trung gian Một số Operations thường sử dụng: filter, map, limit, skip - Ngược lại các operation của Java 8 Stream return một kết quả khác thì được gọi là Terminal Operations Một số Operations: forEach, toArray, min, max, findFirst, anyMatch, allMatch
  • 10. 2. Java Stream Short Circuiting - Các Operations tạo ra các stream hữu hạn (finite stream) cho một stream vô hạn (infinite stream) được gọi là short circuiting. Ex: limit, skip - Các Operations may terminate in finite time for infinite stream. Ex: anyMatch, allMatch, noneMatch, findFirst, findAny
  • 11. 3. Optional - Là một kiểu dữ liệu mới được Java 8 giới thiệu - Là một container object use to contain not-null objects - Optional có nhiều tiện ích để tạo điều kiện cho việc xử lý các giá trị dưới dạng có sẵn hoặc không thay vì việc phải check null
  • 12. 4. Java Time API Why? - Các kiểu data không nhất quán - Các classes Date đều là mutable và không thread safe Đặc điểm: - Tất cả các classes trong new Date Time API đều là inmutable, thread safe - Tách biệt giữa các khái niệm: Date, Time, DateTime, Timestamp, TimeZone - Các method định nghĩa rõ rang - Có thêm các tiện ích cho việc tính toán thời gian - Có thể dễ dàng mở rộng
  • 13. 4. Java Time API Các package trong Java Time API - java.time: LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration - java.time.chrono: Định nghĩa các class không theo chuẩn ISO, có thể extend để tạo hệ thống calendar riêng - java.time.format: Định nghĩa các class sử dụng để formating và parsing - java.time.temporal: Giúp tìm kiếm một số ngày đặc biệt (đầu tháng, cuối tháng,…) - java.time.zone: Hỗ trợ về time zone
  • 14. 5. Một số tính năng khác Collection API improvements Concurrency API improvements Java IO improvements ThreadLocal bổ sung thêm method withInitial(Supplier supplier) Comparator … Tham khảo: https://www.journaldev.com/2389/java-8-features-with-examples

Editor's Notes

  • #5: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #6: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #7: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #8: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #9: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #10: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #11: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #12: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #13: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #14: Định nghĩa theo wiki media https://en.wikipedia.org/wiki/Lambda_expression
  • #15: Tham khảo: https://www.journaldev.com/2389/java-8-features-with-examples
  • #16: Tham khảo: https://www.journaldev.com/2389/java-8-features-with-examples