SlideShare une entreprise Scribd logo
1  sur  27
JDK8
The New Features
Language
Lambdas and functional
interfaces
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello from thread");
}
}).run();
new Thread(() -> System.out.println("Hello from thread")).run()
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
Lambdas and functional
interfaces
Arrays.asList("a", "b", "d").sort((e1, e2) -> e1.compareTo(e2));
Arrays.asList("a", "b", "d").sort(
(String e1, String e2) -> e1.compareTo(e2));
Arrays.asList("a", "b", "d").sort(
(e1, e2) -> {
int result = e1.compareTo(e2);
return result;
});
Default methods
interface DefaultInterface {
void show();
default void display() {
System.out.println("Displaying something");
}
}
But … why ??
Default methods
package java.lang;
public interface Iterable<T> {
void forEach(Consumer<? super T> action);
}
Core interfaces have new methods
This breaks backwards compatibility
package java.lang;
public interface Iterable<T> {
default void forEach(Consumer<? super T> action) {
for (T t : this) { action.accept(t); }
}
}
This doesn’t
Static interface methods
interface MyInterface {
void method1();
static void method2() { System.out.println("Hello from static");
}
}
...
MyInterface.method2();
Keep interface related helper methods in the same class rather
than creating a new one
Method references
Static reference
class Example {
public static void main(String[] args) {
String[] str = {"one", "two", "3", "four"};
Arrays.sort(str, Example::compareByLength);
Arrays.sort(str, (s1, s2) -> s1.length() - s2.length());
}
public static int compareByLength(String s1, String s2) {
return s1.length() - s2.length();
}
}
Method references
Instance reference
@FunctionalInterface // New JDK8 interface
public interface Supplier {
T get();
}
public String function(Supplier<String> supplier) {
return supplier.get();
}
public void example() {
final String x = "A string";
function(() -> x.toString());
function(x::toString);
}
Method references
Constructor reference
class Car {}
class Example {
public static Car createCar(Supplier supplier) {
return supplier.get();
}
public static void repair(Car car) {}
public static void main(String[] args) {
Car car = createCar(Car::new);
List cars = Arrays.asList(car);
cars.forEach(Example::repair);
}
}
Repeating annotations
class RepeatingAnnotations {
public @interface Filters { // A hidden filter holder
Filter[] value();
}
@Repeatable(Filters.class)
public @interface Filter {
String value();
}
@Filter("filter1")
@Filter("filter2")
public void filterredMethod() {}
}
Repeat yourself
Extended annotations
class Annotations {
public @interface NotEmpty {}
public static class Holder<@NonEmpty T> extends @NonEmpty Object{
public void method() throws @NonEmpty Exception {}
}
public static void main(String[] args) {
final Holder<String> holder = new @NonEmpty Holder<String>();
@NonEmpty Collection<@NonEmpty String> strings =
new ArrayList<>();
}
}
Annotate anything
Libraries
Optional
Optional<String> name = Optional.ofNullable(null);
System.out.println("Name is set? " + name.isPresent() );
System.out.println("Name: " + name.orElseGet( () -> "[none]" ));
System.out.println(name.map( s -> "Hey " + s + "!" )
.orElse("Hey Stranger!"));
java.util.Optional
Name is set? false
Name: [none]
Hey Stranger!
Streams
for (Student student : students) {
if (student.getName().startsWith("A")){
names.add(student.getName());
}
}
java.util.stream
List<string> names =
students.stream()
.map(Student::getName)
.filter(name -> name.startsWith("A"))
.collect(Collectors.toList());
The old way
The Java8 way
Streams
java.util.stream
int sum =
students.parallelStream()
.map(Student::getName)
.filter(name -> name.startsWith("A"))
.mapToInt(String::length)
.sum();
System.out.println(
Arrays.asList("One", "Two", "Three")
.stream().collect(Collectors.groupingBy(String::length))
);
{3=[One, Two], 5=[Three]}
Date/Time API
JSR 310 - java.time
Date/Time API
JSR 310 - java.time
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + 2);
LocalTime now = LocalTime.now();
LocalTime later = now.plus(2, HOURS);
The old way
The Java8 way
Nashorn
JavaScript engine
• Rhino replacement
• Faster (2 to 10x performance boost)
• Full ECMAScript 5.1 support + extensions
• Compiles JS to Java bytecode
Nashorn
Example
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
System.out.println(engine.getClass().getName() );
System.out.println("Result:" + engine.eval(
"function f() { return 1; }; f() + 1;" ) );
jdk.nashorn.api.scripting.NashornScriptEngine
Result: 2
Nashorn
Performance
V8 Chrome 31
Spidermonkey F
F 25.0.1
Nashorn
Octane 2.0 14 474 10 066 9 307
Sunspider 1.0.2 220.8 ms 246.5 ms 256.2 ms
http://wnameless.wordpress.com/2013/12/10/javascript-engine-benchmarks-nashorn-vs-v8-vs-spidermonkey/
Base64
Finally … java.util.Base64
final String encoded = Base64
.getEncoder()
.encodeToString( text.getBytes( StandardCharsets.UTF_8 ));
final String decoded = new String(
Base64.getDecoder().decode(encoded),
StandardCharsets.UTF_8);
Tools
jjs
function f() {
return 1;
};
print(f() + 1);
jjs file.js
2
file.js
command
output
Nashorn runner
jdepts
Dependency analyzer
jdeps jdom2-2.0.5.jar
jdom2-2.0.5.jar ->
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/
rt.jar
org.jdom2 (jdom2-2.0.5.jar)
-> java.io
-> java.lang
-> java.net
-> java.util
-> java.util.concurrent
org.jdom2.adapters (jdom2-2.0.5.jar)
-> java.lang
-> java.lang.reflect
-> javax.xml.parsers
…
command
output
JVM
Metaspace
Thank you :)

Contenu connexe

Tendances

Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CAlexis Gallagher
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructuresmartha leon
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML SchemaRaji Ghawi
 
Understanding static analysis php amsterdam 2018
Understanding static analysis   php amsterdam 2018Understanding static analysis   php amsterdam 2018
Understanding static analysis php amsterdam 2018Damien Seguy
 
Developing Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginnersDeveloping Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginnersSaeid Zebardast
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202Mahmoud Samir Fayed
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and UtilitiesPramod Kumar
 

Tendances (20)

Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Collection Core Concept
Collection Core ConceptCollection Core Concept
Collection Core Concept
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Initial Java Core Concept
Initial Java Core ConceptInitial Java Core Concept
Initial Java Core Concept
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructures
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML Schema
 
Xxxx
XxxxXxxx
Xxxx
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
JDBC Core Concept
JDBC Core ConceptJDBC Core Concept
JDBC Core Concept
 
Understanding static analysis php amsterdam 2018
Understanding static analysis   php amsterdam 2018Understanding static analysis   php amsterdam 2018
Understanding static analysis php amsterdam 2018
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
 
Developing Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginnersDeveloping Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginners
 
DCN Practical
DCN PracticalDCN Practical
DCN Practical
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 

En vedette

new features in jdk8
new features in jdk8new features in jdk8
new features in jdk8岩 夏
 
New features in Java 7
New features in Java 7New features in Java 7
New features in Java 7Girish Manwani
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new featuresShivam Goel
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7Deniz Oguz
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 

En vedette (7)

JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
 
new features in jdk8
new features in jdk8new features in jdk8
new features in jdk8
 
New features in Java 7
New features in Java 7New features in Java 7
New features in Java 7
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new features
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 

Similaire à JDK 8

Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Ganesh Samarthyam
 
Nice to meet Kotlin
Nice to meet KotlinNice to meet Kotlin
Nice to meet KotlinJieyi Wu
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIjagriti srivastava
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVMRafael Winterhalter
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistAnton Arhipov
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptxadityaraj7711
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performanceintelliyole
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingHenri Tremblay
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfShashikantSathe3
 

Similaire à JDK 8 (20)

What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Nice to meet Kotlin
Nice to meet KotlinNice to meet Kotlin
Nice to meet Kotlin
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
Java generics
Java genericsJava generics
Java generics
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
Groovy
GroovyGroovy
Groovy
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
Awt
AwtAwt
Awt
 

Dernier

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Dernier (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

JDK 8

  • 3. Lambdas and functional interfaces new Thread(new Runnable() { @Override public void run() { System.out.println("Hello from thread"); } }).run(); new Thread(() -> System.out.println("Hello from thread")).run() @FunctionalInterface public interface Runnable { public abstract void run(); }
  • 4. Lambdas and functional interfaces Arrays.asList("a", "b", "d").sort((e1, e2) -> e1.compareTo(e2)); Arrays.asList("a", "b", "d").sort( (String e1, String e2) -> e1.compareTo(e2)); Arrays.asList("a", "b", "d").sort( (e1, e2) -> { int result = e1.compareTo(e2); return result; });
  • 5. Default methods interface DefaultInterface { void show(); default void display() { System.out.println("Displaying something"); } } But … why ??
  • 6. Default methods package java.lang; public interface Iterable<T> { void forEach(Consumer<? super T> action); } Core interfaces have new methods This breaks backwards compatibility package java.lang; public interface Iterable<T> { default void forEach(Consumer<? super T> action) { for (T t : this) { action.accept(t); } } } This doesn’t
  • 7. Static interface methods interface MyInterface { void method1(); static void method2() { System.out.println("Hello from static"); } } ... MyInterface.method2(); Keep interface related helper methods in the same class rather than creating a new one
  • 8. Method references Static reference class Example { public static void main(String[] args) { String[] str = {"one", "two", "3", "four"}; Arrays.sort(str, Example::compareByLength); Arrays.sort(str, (s1, s2) -> s1.length() - s2.length()); } public static int compareByLength(String s1, String s2) { return s1.length() - s2.length(); } }
  • 9. Method references Instance reference @FunctionalInterface // New JDK8 interface public interface Supplier { T get(); } public String function(Supplier<String> supplier) { return supplier.get(); } public void example() { final String x = "A string"; function(() -> x.toString()); function(x::toString); }
  • 10. Method references Constructor reference class Car {} class Example { public static Car createCar(Supplier supplier) { return supplier.get(); } public static void repair(Car car) {} public static void main(String[] args) { Car car = createCar(Car::new); List cars = Arrays.asList(car); cars.forEach(Example::repair); } }
  • 11. Repeating annotations class RepeatingAnnotations { public @interface Filters { // A hidden filter holder Filter[] value(); } @Repeatable(Filters.class) public @interface Filter { String value(); } @Filter("filter1") @Filter("filter2") public void filterredMethod() {} } Repeat yourself
  • 12. Extended annotations class Annotations { public @interface NotEmpty {} public static class Holder<@NonEmpty T> extends @NonEmpty Object{ public void method() throws @NonEmpty Exception {} } public static void main(String[] args) { final Holder<String> holder = new @NonEmpty Holder<String>(); @NonEmpty Collection<@NonEmpty String> strings = new ArrayList<>(); } } Annotate anything
  • 14. Optional Optional<String> name = Optional.ofNullable(null); System.out.println("Name is set? " + name.isPresent() ); System.out.println("Name: " + name.orElseGet( () -> "[none]" )); System.out.println(name.map( s -> "Hey " + s + "!" ) .orElse("Hey Stranger!")); java.util.Optional Name is set? false Name: [none] Hey Stranger!
  • 15. Streams for (Student student : students) { if (student.getName().startsWith("A")){ names.add(student.getName()); } } java.util.stream List<string> names = students.stream() .map(Student::getName) .filter(name -> name.startsWith("A")) .collect(Collectors.toList()); The old way The Java8 way
  • 16. Streams java.util.stream int sum = students.parallelStream() .map(Student::getName) .filter(name -> name.startsWith("A")) .mapToInt(String::length) .sum(); System.out.println( Arrays.asList("One", "Two", "Three") .stream().collect(Collectors.groupingBy(String::length)) ); {3=[One, Two], 5=[Three]}
  • 17. Date/Time API JSR 310 - java.time
  • 18. Date/Time API JSR 310 - java.time Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + 2); LocalTime now = LocalTime.now(); LocalTime later = now.plus(2, HOURS); The old way The Java8 way
  • 19. Nashorn JavaScript engine • Rhino replacement • Faster (2 to 10x performance boost) • Full ECMAScript 5.1 support + extensions • Compiles JS to Java bytecode
  • 20. Nashorn Example ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); System.out.println(engine.getClass().getName() ); System.out.println("Result:" + engine.eval( "function f() { return 1; }; f() + 1;" ) ); jdk.nashorn.api.scripting.NashornScriptEngine Result: 2
  • 21. Nashorn Performance V8 Chrome 31 Spidermonkey F F 25.0.1 Nashorn Octane 2.0 14 474 10 066 9 307 Sunspider 1.0.2 220.8 ms 246.5 ms 256.2 ms http://wnameless.wordpress.com/2013/12/10/javascript-engine-benchmarks-nashorn-vs-v8-vs-spidermonkey/
  • 22. Base64 Finally … java.util.Base64 final String encoded = Base64 .getEncoder() .encodeToString( text.getBytes( StandardCharsets.UTF_8 )); final String decoded = new String( Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
  • 23. Tools
  • 24. jjs function f() { return 1; }; print(f() + 1); jjs file.js 2 file.js command output Nashorn runner
  • 25. jdepts Dependency analyzer jdeps jdom2-2.0.5.jar jdom2-2.0.5.jar -> /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ rt.jar org.jdom2 (jdom2-2.0.5.jar) -> java.io -> java.lang -> java.net -> java.util -> java.util.concurrent org.jdom2.adapters (jdom2-2.0.5.jar) -> java.lang -> java.lang.reflect -> javax.xml.parsers … command output

Notes de l'éditeur

  1. Functional interface can have only one abstract method Annotation is optional
  2. Real-world functional-style programming into the Java
  3. Parallel stream Arrays, BufferedReader
  4. New API, Safety (enums), Readability