SlideShare a Scribd company logo
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.

More Related Content

What's hot (20)

PDF
Java 8 features
Oleg Tsal-Tsalko
 
PDF
Java8 features
Elias Hasnat
 
PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
PPTX
Java 8 new features
Aniket Thakur
 
PDF
Lambdas in Java 8
Tobias Coetzee
 
PPTX
Functional programming with Java 8
LivePerson
 
PPTX
Actors model in gpars
NexThoughts Technologies
 
ODP
Introduction to Java 8
Knoldus Inc.
 
PDF
Java 8 by example!
Mark Harrison
 
PDF
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 
PPTX
Lambdas and-streams-s ritter-v3
Simon Ritter
 
PDF
Java 8 - Project Lambda
Rahman USTA
 
PPTX
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
PDF
Java SE 8
Simon Ritter
 
PDF
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
 
PDF
Automatic Migration of Legacy Java Method Implementations to Interfaces
Raffi Khatchadourian
 
PPTX
java: basics, user input, data type, constructor
Shivam Singhal
 
PPTX
Java- Updates in java8-Mazenet solution
Mazenetsolution
 
PPT
Java findamentals1
Todor Kolev
 
Java 8 features
Oleg Tsal-Tsalko
 
Java8 features
Elias Hasnat
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Java 8 new features
Aniket Thakur
 
Lambdas in Java 8
Tobias Coetzee
 
Functional programming with Java 8
LivePerson
 
Actors model in gpars
NexThoughts Technologies
 
Introduction to Java 8
Knoldus Inc.
 
Java 8 by example!
Mark Harrison
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 
Lambdas and-streams-s ritter-v3
Simon Ritter
 
Java 8 - Project Lambda
Rahman USTA
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
Java SE 8
Simon Ritter
 
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Raffi Khatchadourian
 
java: basics, user input, data type, constructor
Shivam Singhal
 
Java- Updates in java8-Mazenet solution
Mazenetsolution
 
Java findamentals1
Todor Kolev
 

Similar to Java 8 (20)

PPTX
A brief tour of modern Java
Sina Madani
 
PDF
Java8
Felipe Mamud
 
PPTX
Java 8 presentation
Van Huong
 
PPTX
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
PPTX
java8
Arik Abulafya
 
PDF
Lambda Functions in Java 8
Ganesh Samarthyam
 
PPTX
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
PPTX
Intro to java 8
John Godoi
 
PPT
whats new in java 8
Dori Waldman
 
PDF
Productive Programming in Java 8 - with Lambdas and Streams
Ganesh Samarthyam
 
PDF
Java 8 Workshop
Mario Fusco
 
PDF
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
HackBulgaria
 
PDF
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
Chuk-Munn Lee
 
PDF
Java 8 features
NexThoughts Technologies
 
PDF
Charles Sharp: Java 8 Streams
jessitron
 
PPTX
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
Srimanta Sahu
 
DOCX
Colloquium Report
Mohammad Faizan
 
PPTX
Java_Interview Qns
ManikandanRamanujam
 
PPTX
FUNctional Programming in Java 8
Richard Walker
 
PPTX
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
A brief tour of modern Java
Sina Madani
 
Java 8 presentation
Van Huong
 
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
Lambda Functions in Java 8
Ganesh Samarthyam
 
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
Intro to java 8
John Godoi
 
whats new in java 8
Dori Waldman
 
Productive Programming in Java 8 - with Lambdas and Streams
Ganesh Samarthyam
 
Java 8 Workshop
Mario Fusco
 
Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria
HackBulgaria
 
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
Chuk-Munn Lee
 
Java 8 features
NexThoughts Technologies
 
Charles Sharp: Java 8 Streams
jessitron
 
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
Srimanta Sahu
 
Colloquium Report
Mohammad Faizan
 
Java_Interview Qns
ManikandanRamanujam
 
FUNctional Programming in Java 8
Richard Walker
 
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
Ad

Recently uploaded (20)

PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Ad

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.