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

PPTX
Java 8 - Features Overview
PPTX
java 8 new features
PPTX
Java 8 lambda
PPTX
Java 8 Features
PPTX
Java SE 8 - New Features
PPTX
Lambda Expressions in Java 8
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PPT
Major Java 8 features
Java 8 - Features Overview
java 8 new features
Java 8 lambda
Java 8 Features
Java SE 8 - New Features
Lambda Expressions in Java 8
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Major Java 8 features

What's hot (20)

PDF
Java 8 features
PDF
Java8 features
PDF
Java 8 Lambda Expressions & Streams
PPTX
Java 8 new features
PDF
Lambdas in Java 8
PPTX
Functional programming with Java 8
PPTX
Actors model in gpars
ODP
Introduction to Java 8
PDF
Java 8 by example!
PDF
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
PPTX
Lambdas and-streams-s ritter-v3
PDF
Java 8 - Project Lambda
PPTX
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
PDF
Java SE 8
PDF
Apouc 2014-java-8-create-the-future
PDF
Automatic Migration of Legacy Java Method Implementations to Interfaces
PPTX
java: basics, user input, data type, constructor
PPTX
Java- Updates in java8-Mazenet solution
PPT
Java findamentals1
Java 8 features
Java8 features
Java 8 Lambda Expressions & Streams
Java 8 new features
Lambdas in Java 8
Functional programming with Java 8
Actors model in gpars
Introduction to Java 8
Java 8 by example!
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and-streams-s ritter-v3
Java 8 - Project Lambda
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Java SE 8
Apouc 2014-java-8-create-the-future
Automatic Migration of Legacy Java Method Implementations to Interfaces
java: basics, user input, data type, constructor
Java- Updates in java8-Mazenet solution
Java findamentals1
Ad

Similar to Java 8 (20)

DOCX
Colloquium Report
PDF
Charles Sharp: Java 8 Streams
PDF
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
PDF
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
PDF
PDF
Java 8 Overview
PDF
Lambda.pdf
PDF
Functional programming in java 8 by harmeet singh
PPTX
Software Uni Conf October 2014
PPTX
JDK8 Streams
PPT
Lambdas
PPTX
Java 8 Intro - Core Features
PDF
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
PPTX
Functional Programming With Lambdas and Streams in JDK8
PPTX
objectorientedprogrammingCHAPTER 2 (OOP).pptx
PPTX
New Features in JDK 8
PDF
2014 java functional
PPTX
Java 8 New features
PPT
Java findamentals1
Colloquium Report
Charles Sharp: Java 8 Streams
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Java 8 Overview
Lambda.pdf
Functional programming in java 8 by harmeet singh
Software Uni Conf October 2014
JDK8 Streams
Lambdas
Java 8 Intro - Core Features
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
A Brief Conceptual Introduction to Functional Java 8 and its API
Functional Programming With Lambdas and Streams in JDK8
objectorientedprogrammingCHAPTER 2 (OOP).pptx
New Features in JDK 8
2014 java functional
Java 8 New features
Java findamentals1
Ad

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
AI in Product Development-omnex systems
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Digital Strategies for Manufacturing Companies
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
history of c programming in notes for students .pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Nekopoi APK 2025 free lastest update
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Introduction to Artificial Intelligence
Upgrade and Innovation Strategies for SAP ERP Customers
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Operating system designcfffgfgggggggvggggggggg
AI in Product Development-omnex systems
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Digital Strategies for Manufacturing Companies
VVF-Customer-Presentation2025-Ver1.9.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Wondershare Filmora 15 Crack With Activation Key [2025
Softaken Excel to vCard Converter Software.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
history of c programming in notes for students .pptx
top salesforce developer skills in 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Nekopoi APK 2025 free lastest update
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 41
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Introduction to Artificial Intelligence

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.