SlideShare uma empresa Scribd logo
1 de 29
Java 8
A new programming paradigm in java.
✓Default method
✓Lambda Overview
✓Stream API
Topics
Java provides a facility to create default methods inside the interface.
Java Default Methods
➢ Improve scalability.
➢ Added new method without hampering current implementation.
➢ Implementation flexibility.
Why Default Method?
What-if the multiple interfaces have default methods with the same
signatures.
Rule 1 – Classes take higher precedence than interfaces
Default method conflict
• Rule 2 – Derived interfaces or sub-interfaces take higher precedence than
the interfaces higher-up in the inheritance hierarchy.
Default method conflict
In the above class diagram, interface B inherits from interface A. Both have a default method
print() with the same signature. Class C implements both interfaces A & B. When print()
method is invoked on an instance of class C then the implementation in interface B is invoked
as it is the lowest child/most derived interface in the inheritance hierarchy.
• Rule 3 – In case Rule 1 and Rule 2 are not able to resolve the conflict then the
implementing class has to specifically override and provide a method with the
same method definition.
Java 8 Features
In the above class diagram, class C inherits from interfaces A & B, both of which have the
default implementations of print(). Since, both interfaces A & B are parents of C, they are at
the same hierarchy level, and hence, C has to provide its own implementation of method
print().
B.super.print()
• Scenario 1 of diamond problem in Java 8
Diamond Problem
Java 8 resolves this situation by considering that there is only one implementation of
print() method, which is in class Alpha. Hence, when Delta invokes print() then the
implementation of print() in Alpha is executed.
• Scenario 2 of diamond problem in Java 8 –
Diamond Problem
The answer lies in Rule 3 of the conflict resolution
Lambda Overview
Why Lambdas?
➢ Enable functional programming.
➢ Readable and concise code.
➢ Easier to use APIs and libraries.
➢ Enables support for parallel processing.
Functional Interface
@FunctionalInterface
public interface Greeter{
void greet()
}
➢ Oracle Doc
https://docs.oracle.com/javase/8/docs/api/?java/util/function/package-summary.html
Inline values
String name = “SK Paik";
int age = 32;
➢Can we assign a block of code to a variable as values?
➢Can we do something that?
<Interface> aBlockOfCode = {
................
................
}
Yes by lambda we can do that.
Expression
Methods
public void greet()
{
System.out.print(“Hello”);
}
public int add(int a, int b)
{
return a+b;
}
publuc int getLength(String s)
{
return s.lenght();
}
Lambda Expression
greetExp = ()->System.out.print(“Hello”);
addExp = (int a, int b)->return a+b;
addExp = ( a, b)->return a+b;
lengthExp = (String s) ->s.lenght();
divExp = (int a, int b)->{
if(b==0) return 0;
return a/b;
}
Type Inference
Interface Greet{
void greet();
}
Interface Add{
Int add(int a, int b);
}
Interface Length{
Int getLength(String s);
}
Lambda Expression of above interface
Greet greetExp = ()->System.out.print(“”)}
Add addExp = (int n,int m)->n+m;
Length lenghtExp = (String s)->s.lenght();
Runnable In Lambda
Public void simpleThread(){
Thread thread = new Thread(new Runnable(){
Public void run(){
System.out.println(“Hello From Runnable”);
}
});
Thread.start();
}
Public void simpleThread(){
Thread thread = new Thread(()->System.out.print(“Hello lambda));
thread.start();
}
Stream represents a sequence of objects from a source, which supports
aggregate operations. Following are the characteristics of a Stream.
• Sequence of elements − A stream provides a set of elements of specific
type in a sequential manner. A stream gets/computes elements on
demand. It never stores the elements.
• Source − Stream takes Collections, Arrays, or I/O resources as input
source.
• Aggregate operations − Stream supports aggregate operations like filter,
map, limit, reduce, find, match, and so on.
• Pipelining − Most of the stream operations return stream itself so that
their result can be pipelined.
• Automatic iterations − Stream operations do the iterations internally over
the source elements provided, in contrast to Collections where explicit
iteration is required.
Java Stream
• Before we look into Java Stream API Examples, let’s see why it was
required. Suppose we want to iterate over a list of integers and find out
sum of all the integers greater than 10.
Java Stream
1. Program is handling the algorithm to
iterate over the list.
2. The program is sequential in nature,
there is no way we can do this in parallel
easily.
3. There is a lot of code to do even a simple
task.
Using Java Stream API
• Collections:
A collection is an in-memory data structure to hold values and before we
start using collection, all the values should have been populated.
• Java Stream
Whereas a java Stream is a data structure that is computed on-demand. Java
Stream doesn’t store data, it operates on the source data structure (collection and
array) and produce pipelined data that we can use and perform specific
operations.
Collections and Java Stream
With Java 8, Collection interface has two methods to generate a
Stream.
• stream() − Returns a sequential stream considering collection
as its source.
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
• parallelStream() − Returns a parallel Stream considering
collection as its source.
Stream Type
• We can use Stream.of() to create a stream from similar type
of data.
Creating Java Streams
• We can use Stream.of() with an array of Objects to return the stream.
• We can use Collection stream() to create sequential stream and parallelStream() to
create parallel stream.
Creating Java Streams
We can use Stream.generate() and Stream.iterate() methods to create Stream.
We can use java Stream collect() method to get List, Map or Set from stream.
• Intermediate Operations
map(), reduce(), filter(), limit()
• Terminal Operations
Count(), sum(), findAny()
• Short-circuit operation
findFirst(), anyMatch();
Java Stream Operations
• Stream filter() example: We can use filter() method to test stream elements for a
condition and generate filtered list.
Java Stream Intermediate Operations
Java Terminal Operations
• Stream count() example: We can use this terminal operation to count the
number of items in the stream.
Short-circuit operation
Java 8 short-circuiting operations are just like boolean short-circuit evaluations in
Java.
Java 8 Stream short-circuit operations are not limited to boolean types. There are pre
defined short-circuiting operations.
Intermediate short-circuiting methods
Stream<T> limit(long maxSize)
Terminal short-circuiting methods
Optional<T> findFirst()
Stream pipelines
• Expressing a stream pipeline as a series of functional transformations enables
several useful execution strategies, such as laziness, parallelism, short-circuiting,
and operation fusion.
• Not Reusable: Once a Stream is consumed, it can’t be used later
on. As you can see in above examples that every time I am creating
a stream.
• Performance: A for loop through an array is extremely lightweight
both in terms of heap and CPU usage. If raw speed and memory
thriftiness is a priority, using a stream is worse.
• Familiarity. The world is full of experienced procedural
programmers, from many language backgrounds, for whom loops
are familiar and streams are novel. In some environments, you
want to write code that's familiar to that kind of person.
• Debuggers are improving, but even now, when you're stepping
through stream code in a debugger, it can be harder work than the
equivalent loop, because a simple loop is very close to the
variables and code locations that a traditional debugger works
with.
Stream API Limitations
Thanks
Azizul Islam
Senior Software Engineer
W3Engineers Ltd.

Mais conteúdo relacionado

Mais procurados

Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8Knoldus Inc.
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterJAXLondon2014
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Simon Ritter
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaSimon Ritter
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesRaffi Khatchadourian
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructorShivam Singhal
 
Java- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solutionJava- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solutionMazenetsolution
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 

Mais procurados (20)

Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
 
Java SE 8
Java SE 8Java SE 8
Java SE 8
 
Apouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-futureApouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-future
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Java- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solutionJava- Updates in java8-Mazenet solution
Java- Updates in java8-Mazenet solution
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 

Semelhante a Java 8

Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Raffi Khatchadourian
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014Nayden Gochev
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...Raffi Khatchadourian
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8IndicThreads
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New featuresSon Nguyen
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 

Semelhante a Java 8 (20)

Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
 
Java8
Java8Java8
Java8
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
 
Lambda.pdf
Lambda.pdfLambda.pdf
Lambda.pdf
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
 
Lambdas
LambdasLambdas
Lambdas
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
2014 java functional
2014 java functional2014 java functional
2014 java functional
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java 8
Java 8Java 8
Java 8
 

Último

%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
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
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 

Último (20)

%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
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
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Java 8

  • 1. Java 8 A new programming paradigm in java.
  • 3. Java provides a facility to create default methods inside the interface. Java Default Methods
  • 4. ➢ Improve scalability. ➢ Added new method without hampering current implementation. ➢ Implementation flexibility. Why Default Method?
  • 5. What-if the multiple interfaces have default methods with the same signatures. Rule 1 – Classes take higher precedence than interfaces Default method conflict
  • 6. • Rule 2 – Derived interfaces or sub-interfaces take higher precedence than the interfaces higher-up in the inheritance hierarchy. Default method conflict In the above class diagram, interface B inherits from interface A. Both have a default method print() with the same signature. Class C implements both interfaces A & B. When print() method is invoked on an instance of class C then the implementation in interface B is invoked as it is the lowest child/most derived interface in the inheritance hierarchy.
  • 7. • Rule 3 – In case Rule 1 and Rule 2 are not able to resolve the conflict then the implementing class has to specifically override and provide a method with the same method definition. Java 8 Features In the above class diagram, class C inherits from interfaces A & B, both of which have the default implementations of print(). Since, both interfaces A & B are parents of C, they are at the same hierarchy level, and hence, C has to provide its own implementation of method print(). B.super.print()
  • 8. • Scenario 1 of diamond problem in Java 8 Diamond Problem Java 8 resolves this situation by considering that there is only one implementation of print() method, which is in class Alpha. Hence, when Delta invokes print() then the implementation of print() in Alpha is executed.
  • 9. • Scenario 2 of diamond problem in Java 8 – Diamond Problem The answer lies in Rule 3 of the conflict resolution
  • 10. Lambda Overview Why Lambdas? ➢ Enable functional programming. ➢ Readable and concise code. ➢ Easier to use APIs and libraries. ➢ Enables support for parallel processing.
  • 11. Functional Interface @FunctionalInterface public interface Greeter{ void greet() } ➢ Oracle Doc https://docs.oracle.com/javase/8/docs/api/?java/util/function/package-summary.html
  • 12. Inline values String name = “SK Paik"; int age = 32; ➢Can we assign a block of code to a variable as values? ➢Can we do something that? <Interface> aBlockOfCode = { ................ ................ } Yes by lambda we can do that.
  • 13. Expression Methods public void greet() { System.out.print(“Hello”); } public int add(int a, int b) { return a+b; } publuc int getLength(String s) { return s.lenght(); } Lambda Expression greetExp = ()->System.out.print(“Hello”); addExp = (int a, int b)->return a+b; addExp = ( a, b)->return a+b; lengthExp = (String s) ->s.lenght(); divExp = (int a, int b)->{ if(b==0) return 0; return a/b; }
  • 14. Type Inference Interface Greet{ void greet(); } Interface Add{ Int add(int a, int b); } Interface Length{ Int getLength(String s); } Lambda Expression of above interface Greet greetExp = ()->System.out.print(“”)} Add addExp = (int n,int m)->n+m; Length lenghtExp = (String s)->s.lenght();
  • 15. Runnable In Lambda Public void simpleThread(){ Thread thread = new Thread(new Runnable(){ Public void run(){ System.out.println(“Hello From Runnable”); } }); Thread.start(); } Public void simpleThread(){ Thread thread = new Thread(()->System.out.print(“Hello lambda)); thread.start(); }
  • 16. Stream represents a sequence of objects from a source, which supports aggregate operations. Following are the characteristics of a Stream. • Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements. • Source − Stream takes Collections, Arrays, or I/O resources as input source. • Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on. • Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. • Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required. Java Stream
  • 17. • Before we look into Java Stream API Examples, let’s see why it was required. Suppose we want to iterate over a list of integers and find out sum of all the integers greater than 10. Java Stream 1. Program is handling the algorithm to iterate over the list. 2. The program is sequential in nature, there is no way we can do this in parallel easily. 3. There is a lot of code to do even a simple task.
  • 19. • Collections: A collection is an in-memory data structure to hold values and before we start using collection, all the values should have been populated. • Java Stream Whereas a java Stream is a data structure that is computed on-demand. Java Stream doesn’t store data, it operates on the source data structure (collection and array) and produce pipelined data that we can use and perform specific operations. Collections and Java Stream
  • 20. With Java 8, Collection interface has two methods to generate a Stream. • stream() − Returns a sequential stream considering collection as its source. List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); • parallelStream() − Returns a parallel Stream considering collection as its source. Stream Type
  • 21. • We can use Stream.of() to create a stream from similar type of data. Creating Java Streams • We can use Stream.of() with an array of Objects to return the stream. • We can use Collection stream() to create sequential stream and parallelStream() to create parallel stream.
  • 22. Creating Java Streams We can use Stream.generate() and Stream.iterate() methods to create Stream. We can use java Stream collect() method to get List, Map or Set from stream.
  • 23. • Intermediate Operations map(), reduce(), filter(), limit() • Terminal Operations Count(), sum(), findAny() • Short-circuit operation findFirst(), anyMatch(); Java Stream Operations
  • 24. • Stream filter() example: We can use filter() method to test stream elements for a condition and generate filtered list. Java Stream Intermediate Operations
  • 25. Java Terminal Operations • Stream count() example: We can use this terminal operation to count the number of items in the stream.
  • 26. Short-circuit operation Java 8 short-circuiting operations are just like boolean short-circuit evaluations in Java. Java 8 Stream short-circuit operations are not limited to boolean types. There are pre defined short-circuiting operations. Intermediate short-circuiting methods Stream<T> limit(long maxSize) Terminal short-circuiting methods Optional<T> findFirst()
  • 27. Stream pipelines • Expressing a stream pipeline as a series of functional transformations enables several useful execution strategies, such as laziness, parallelism, short-circuiting, and operation fusion.
  • 28. • Not Reusable: Once a Stream is consumed, it can’t be used later on. As you can see in above examples that every time I am creating a stream. • Performance: A for loop through an array is extremely lightweight both in terms of heap and CPU usage. If raw speed and memory thriftiness is a priority, using a stream is worse. • Familiarity. The world is full of experienced procedural programmers, from many language backgrounds, for whom loops are familiar and streams are novel. In some environments, you want to write code that's familiar to that kind of person. • Debuggers are improving, but even now, when you're stepping through stream code in a debugger, it can be harder work than the equivalent loop, because a simple loop is very close to the variables and code locations that a traditional debugger works with. Stream API Limitations
  • 29. Thanks Azizul Islam Senior Software Engineer W3Engineers Ltd.