New Features in Java 8
05/05/2025
1
Learning Objectives
After this session you will be able to understand:
Java 8 new Features.
Default and Static methods in Interfaces.
Functional Interfaces.
Lambda Expressions.
Using Lambda with Collection API
05/05/2025
2
Java 8 New Features
Features:
Default and static methods in interfaces.
Functional interfaces.
Lambda expressions.
Java stream API for bulk data operations on Collections.
Java Time API.
forEach() method in Iterable interface.
Collection API improvements.
Java IO improvements.
05/05/2025
3
Java 8 New Features
Interface Default Method Support
Prior to Java 8, we could have only method declarations in the
interfaces
• In Java 8, we can have default methods and static methods in the
interfaces.
• Java interface default methods will help us in extending interfaces without
having the fear of breaking implementation classes.
• One of the major reason for introducing default methods in interfaces is to
enhance the Collections API in Java 8.
• default methods are implemented in Java 8 to support lambda expressions.
• Java interface default methods are also referred to as Defender Methods or
Virtual extension methods.
• For creating a default method in Java interface, we need to use “default”
keyword with the method signature.
05/05/2025
4
Java 8 New Features
Interface Static Method Support
• Java interface static method is similar to default method except that we
can’t override them in the implementation classes.
• Java interface static method is part of interface, we can’t use it for
implementation class objects.
• Java interface static methods are good for providing utility methods
e.g. Checking nulls, sorting collection etc.
• Java interface static method helps us in providing security by not allowing
implementation classes to override them.
• We can use Java interface static methods to remove utility classes such
as Collections class. e.g. [Link]() method can be implemented in
interface directly (code management becomes easy)
05/05/2025
5
Java 8 New Features
interface default and static method support
Demo on static method and default method usage:
public interface INullTest { Ensuring string data
default void getDataVarified(String strData) { before use by
if (! isNull(strData )){ Checking
[Link]("data validated:"+strData); null and blank
} string
} //Default Method
public static boolean isNull(String strData) {
return strData == null ? true : "".equals(strData) ? true : false; }
}//End of INullTest
public class ClientTest implements INullTest {
public static void main(String args[]) {
String data="welcome";
ClientTest clientObject= new ClientTest();
[Link](data); } //main end
} //End of ClientTest class
05/05/2025
6
Java 8 New Features
Functional Interfaces
• An interface with exactly one abstract method is known as Functional
Interface.
• A new annotation @FunctionalInterface has been introduced to mark an
interface as Functional Interface.
• The major benefit of interfaces is that we can use lambda expressions to
instantiate them and avoid using bulky anonymous class implementation.
Runnable
Comparable Functional interface
Comparator
Java 8 api new package [Link] gives us Functional interfacs
1. Consumer 2. Supplier
3. Function 4. Predicate (Functional interface in Java 8
API)
05/05/2025
7
Java 8 New Features
Functional Interfaces
public interface Iwork {
Public void doThePreWork();
} //End of interface public interface Iwork {
public void doThePreWork();
public void doThePostWork();
} //End of interface
@FunctionalInterface
public interface Iwork {
public void doThePreWork();
public void doThePostWork();
} //End of interface
Compiler
Notifies the
Lambda expression Supports Error at compile
Functional interfaces time
05/05/2025
8
Java 8 New Features
Using Functional Interfaces Predicate
and Lambda Expressions .
Predicate interface is defined in java [Link] package :
Condition 1: Find Employees who are male and age more than 20
Predicate<Employee> maleEmployee = s-> [Link] >20 &&
"male".equals([Link]);
Condition 2: All Employees who are female and age more than 18
Predicate<Employee> femaleEmployee = s-> [Link] >18 &&
“female".equals([Link]);
05/05/2025
9
Java 8 New Features
Lambda Expressions.
“ An Expression representing an anonymous functionality”
Lambda Expression consist of 3 parts
-Set of Parameters
-Lambda Operator (->)
-Function Body
Using LamdaExpression on function with no parameter
() -> { function body statemetns }
Using LambdaExpression on function with parameters
(parameterName) -> { statements;};
(parameterType paramterename) -> {statements; return type;}
05/05/2025
10
Java 8 New Features
Lambda Expressions.
Matching Lambda Expression:
1) Does the interface has only one method (FunctionalInterface check).
2) Does the parameter of lambda expression matches the paramter of
method in interface.
3) Does the return type of lambda expression matches the return type of
method in interface.
interface IMessanger{
public void message(); } end lMessanger
public class TestClient {
public static void main(String arg[]) {
IMessanger ref= ()->{[Link]("message is given to client");};
[Link]();} } //End of TestClient
05/05/2025
11
New Features in Java
Lambda Expressions and collections
List<String> names= new ArrayList();
[Link]("jack"); [Link]("ami"); [Link]("bhalchadra");
[Link](names , new Comparator<String>(){
public int compare(String s1 , String s2) {
return [Link](s1);}});
for(String name : names) before
{ [Link](name); } Java 8
[Link](names,(s1,s2)->[Link](s1));
[Link](name->[Link](name));
After Java
8
05/05/2025
12
Thank you!!
13