SlideShare a Scribd company logo
New Features in JDK 8
Yann.xia
CONTENTS
01
Lambda Expressions
02
Stream API
03
Method Ref
04
Others
01Lambda Expressions
Lambda
Expressions
• Lambdas 匿名表达式 (JSR 335)
• Example
(paramters) -> {body}
(x, y) -> x + y
Lambda
Expressions
匿名类 Lambdas
new Thread(new Runnable() {
@Override public void run() {
System.out.print("hi");
}
}).run();
new Thread(() -> System.out.print("hi")).run();
Lambda
Expressions
Multiset<Integer> lengths = HashMultiset.create(
FluentIterable.from(strings)
.filter(new Predicate<String>() {
public boolean apply(String string) {
return
CharMatcher.JAVA_UPPER_CASE.matchesAllOf(string);
}
})
.transform(new Function<String,
Integer>() {
public Integer apply(String string) {
return string.length();
}
}));
匿名类 Lambdas
Multiset<Integer> lengths = HashMultiset.create(
FluentIterable.from(strings)
.filter(string ->
CharMatcher.JAVA_UPPER_CASE.matchesAllOf(string))
.transform(string -> string.length()));
02 Stream API
Stream API
Common Stream
List<Transaction> groceryTransactions = new Arraylist<>();
for(Transaction t: transactions){
if(t.getType() == Transaction.GROCERY){
groceryTransactions.add(t);
}
}
Collections.sort(groceryTransactions, new Comparator(){
public int compare(Transaction t1, Transaction t2){
return t2.getValue().compareTo(t1.getValue());
}
});
List<Integer> transactionIds = new ArrayList<>();
for(Transaction t: groceryTransactions){
transactionsIds.add(t.getId());
}
List<Integer> transactionsIds =
transactions.stream()
.filter(t -> t.getType() == Transaction.GROCERY)
.sorted(comparing(Transaction::getValue).reversed())
.map(Transaction::getId)
.collect(toList());
Stream API
Stream API
03Method References
Method References
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 15, 16);
List<Integer> primeNumbers = numbers.stream().filter((x) ->
isPrime(x)).collect(Collectors.toList());
System.out.println("Prime Numbers are " + primeNumbers);
}
public static boolean isPrime(int number) {
/*…*/
}
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 15, 16);
List<Integer> primeNumbers =
numbers.stream().filter(ReferenceToStaticMethodExample::isPr
ime).collect(Collectors.toList());
System.out.println("Prime Numbers are " + primeNumbers);
}
Method References
public static void main(String args[]){
List names = new ArrayList();
names.add("Mahesh");
names.add("Suresh");
names.add("Ramesh");
names.add("Naresh");
names.add("Kalpesh");
names.forEach(System.out::println);
}
public static void main(String args[]){
List names = new ArrayList();
names.add("Mahesh");
names.add("Suresh");
names.add("Ramesh");
names.add("Naresh");
names.add("Kalpesh");
for (Object name : names){
System.out.print(name);
}
}
04 Others
Others- Date &
Time
//Current Date
LocalDate today = LocalDate.now();
System.out.println("Current Date=" + today);
//Current Time
LocalTime time = LocalTime.now();
System.out.println("Current Time=" + time);
//Current Date
LocalDateTime today = LocalDateTime.now();
System.out.println("Current DateTime=" + today);
Others – default &
static
interface FunctionalDefaultMethods {
void method(); //common
default void defaultMethod() {
System.out.print(this);
//default method
}
static void staticMethod() {
// static method
}
}
Others – Repeatable
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Filters {
Filter[] value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Filters.class)
public @interface Filter {
String value();
}
@Filter("filter1")
@Filter("filter2")
public interface Filterable {
}
Others – Param Name
class ParameterNames {
public static void main(String[] args) throws Exception {
Method method =
ParameterNames.class.getMethod("main", String[].class);
for (final Parameter parameter : method.getParameters())
{
System.out.println("Parameter: " +
parameter.getName());
}
}
}
编译时增加 –parameters
THANKS FOR WATCHING

More Related Content

PPTX
Lambda выражения и Java 8
PPTX
Object Oriented JavaScript
PDF
Java 8 Lambda Expressions
PPTX
Collection
PDF
Java 8 - project lambda
ODP
Functions & closures
PDF
The Ring programming language version 1.8 book - Part 40 of 202
PPTX
Java gets a closure
Lambda выражения и Java 8
Object Oriented JavaScript
Java 8 Lambda Expressions
Collection
Java 8 - project lambda
Functions & closures
The Ring programming language version 1.8 book - Part 40 of 202
Java gets a closure

What's hot (20)

PPTX
Anti patterns
PPTX
Lambda expressions
PPTX
Java 8 Lambda Expressions
PPTX
Pattern Matching in Java 14
PPTX
Java 8 Intro - Core Features
PPTX
02 Java Language And OOP Part II LAB
PDF
Java 8 lambda expressions
PDF
The Ring programming language version 1.6 book - Part 37 of 189
PPT
Core java by a introduction sandesh sharma
PDF
Java 8 Stream API. A different way to process collections.
ODP
Functional programming with Scala
PPT
(Ai lisp)
PDF
The Ring programming language version 1.7 book - Part 39 of 196
PDF
JavaOne 2016 - Learn Lambda and functional programming
PPTX
Java 8, lambdas, generics: How to survive? - NYC Java Meetup Group
PDF
The Ring programming language version 1.5.2 book - Part 31 of 181
PDF
Scala is java8.next()
ODP
Xml processing in scala
PDF
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
PDF
Functional programming in java
Anti patterns
Lambda expressions
Java 8 Lambda Expressions
Pattern Matching in Java 14
Java 8 Intro - Core Features
02 Java Language And OOP Part II LAB
Java 8 lambda expressions
The Ring programming language version 1.6 book - Part 37 of 189
Core java by a introduction sandesh sharma
Java 8 Stream API. A different way to process collections.
Functional programming with Scala
(Ai lisp)
The Ring programming language version 1.7 book - Part 39 of 196
JavaOne 2016 - Learn Lambda and functional programming
Java 8, lambdas, generics: How to survive? - NYC Java Meetup Group
The Ring programming language version 1.5.2 book - Part 31 of 181
Scala is java8.next()
Xml processing in scala
Icsug dev day2014_road to damascus - conversion experience-lotusscript and @f...
Functional programming in java
Ad

Similar to new features in jdk8 (20)

PPTX
New Features in JDK 8
PPTX
What is new in Java 8
PDF
What's new in java 8
PDF
The... Wonderful? World of Lambdas
PPT
Major Java 8 features
PDF
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
PDF
Java 8 Workshop
PDF
Sailing with Java 8 Streams
PPTX
New features in jdk8 iti
PPTX
Java8: what's new and what's hot
PPT
Xm lparsers
PDF
PDF
Real Time Big Data Management
PPTX
Java 8 lambda
PPTX
It's Java, Jim, but not as we know it
PPTX
Java 8 Bootcamp
PDF
SOLID mit Java 8
PDF
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
PDF
Productive Programming in Java 8 - with Lambdas and Streams
PDF
Java 8 features
New Features in JDK 8
What is new in Java 8
What's new in java 8
The... Wonderful? World of Lambdas
Major Java 8 features
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Java 8 Workshop
Sailing with Java 8 Streams
New features in jdk8 iti
Java8: what's new and what's hot
Xm lparsers
Real Time Big Data Management
Java 8 lambda
It's Java, Jim, but not as we know it
Java 8 Bootcamp
SOLID mit Java 8
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Productive Programming in Java 8 - with Lambdas and Streams
Java 8 features
Ad

Recently uploaded (20)

PDF
Softaken Excel to vCard Converter Software.pdf
PDF
System and Network Administraation Chapter 3
PDF
medical staffing services at VALiNTRY
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
history of c programming in notes for students .pptx
PPTX
Transform Your Business with a Software ERP System
PDF
Digital Strategies for Manufacturing Companies
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Softaken Excel to vCard Converter Software.pdf
System and Network Administraation Chapter 3
medical staffing services at VALiNTRY
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Upgrade and Innovation Strategies for SAP ERP Customers
Design an Analysis of Algorithms I-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
CHAPTER 2 - PM Management and IT Context
history of c programming in notes for students .pptx
Transform Your Business with a Software ERP System
Digital Strategies for Manufacturing Companies
wealthsignaloriginal-com-DS-text-... (1).pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Choose the Right IT Partner for Your Business in Malaysia
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems

new features in jdk8

  • 1. New Features in JDK 8 Yann.xia
  • 4. Lambda Expressions • Lambdas 匿名表达式 (JSR 335) • Example (paramters) -> {body} (x, y) -> x + y
  • 5. Lambda Expressions 匿名类 Lambdas new Thread(new Runnable() { @Override public void run() { System.out.print("hi"); } }).run(); new Thread(() -> System.out.print("hi")).run();
  • 6. Lambda Expressions Multiset<Integer> lengths = HashMultiset.create( FluentIterable.from(strings) .filter(new Predicate<String>() { public boolean apply(String string) { return CharMatcher.JAVA_UPPER_CASE.matchesAllOf(string); } }) .transform(new Function<String, Integer>() { public Integer apply(String string) { return string.length(); } })); 匿名类 Lambdas Multiset<Integer> lengths = HashMultiset.create( FluentIterable.from(strings) .filter(string -> CharMatcher.JAVA_UPPER_CASE.matchesAllOf(string)) .transform(string -> string.length()));
  • 8. Stream API Common Stream List<Transaction> groceryTransactions = new Arraylist<>(); for(Transaction t: transactions){ if(t.getType() == Transaction.GROCERY){ groceryTransactions.add(t); } } Collections.sort(groceryTransactions, new Comparator(){ public int compare(Transaction t1, Transaction t2){ return t2.getValue().compareTo(t1.getValue()); } }); List<Integer> transactionIds = new ArrayList<>(); for(Transaction t: groceryTransactions){ transactionsIds.add(t.getId()); } List<Integer> transactionsIds = transactions.stream() .filter(t -> t.getType() == Transaction.GROCERY) .sorted(comparing(Transaction::getValue).reversed()) .map(Transaction::getId) .collect(toList());
  • 12. Method References public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16); List<Integer> primeNumbers = numbers.stream().filter((x) -> isPrime(x)).collect(Collectors.toList()); System.out.println("Prime Numbers are " + primeNumbers); } public static boolean isPrime(int number) { /*…*/ } public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16); List<Integer> primeNumbers = numbers.stream().filter(ReferenceToStaticMethodExample::isPr ime).collect(Collectors.toList()); System.out.println("Prime Numbers are " + primeNumbers); }
  • 13. Method References public static void main(String args[]){ List names = new ArrayList(); names.add("Mahesh"); names.add("Suresh"); names.add("Ramesh"); names.add("Naresh"); names.add("Kalpesh"); names.forEach(System.out::println); } public static void main(String args[]){ List names = new ArrayList(); names.add("Mahesh"); names.add("Suresh"); names.add("Ramesh"); names.add("Naresh"); names.add("Kalpesh"); for (Object name : names){ System.out.print(name); } }
  • 15. Others- Date & Time //Current Date LocalDate today = LocalDate.now(); System.out.println("Current Date=" + today); //Current Time LocalTime time = LocalTime.now(); System.out.println("Current Time=" + time); //Current Date LocalDateTime today = LocalDateTime.now(); System.out.println("Current DateTime=" + today);
  • 16. Others – default & static interface FunctionalDefaultMethods { void method(); //common default void defaultMethod() { System.out.print(this); //default method } static void staticMethod() { // static method } }
  • 17. Others – Repeatable @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Filters { Filter[] value(); } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Repeatable(Filters.class) public @interface Filter { String value(); } @Filter("filter1") @Filter("filter2") public interface Filterable { }
  • 18. Others – Param Name class ParameterNames { public static void main(String[] args) throws Exception { Method method = ParameterNames.class.getMethod("main", String[].class); for (final Parameter parameter : method.getParameters()) { System.out.println("Parameter: " + parameter.getName()); } } } 编译时增加 –parameters