[go: up one dir, main page]

0% found this document useful (0 votes)
8 views13 pages

Java 8 Features Mahendra

Java 8 introduces several new features including default and static methods in interfaces, functional interfaces, and lambda expressions. It enhances the Collections API with the Java stream API for bulk data operations and introduces the Java Time API. Additionally, improvements in the Collection and IO APIs are highlighted, along with practical examples of using lambda expressions with functional interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Java 8 Features Mahendra

Java 8 introduces several new features including default and static methods in interfaces, functional interfaces, and lambda expressions. It enhances the Collections API with the Java stream API for bulk data operations and introduces the Java Time API. Additionally, improvements in the Collection and IO APIs are highlighted, along with practical examples of using lambda expressions with functional interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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. Collections.sort() 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
System.out.println("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();
clientObject.getDataVarified(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 java.util.function 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 util.function package :

Condition 1: Find Employees who are male and age more than 20

Predicate<Employee> maleEmployee = s-> s.age >20 &&


"male".equals(s.gender);

Condition 2: All Employees who are female and age more than 18

Predicate<Employee> femaleEmployee = s-> s.age >18 &&


“female".equals(s.gender);

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= ()->{System.out.println("message is given to client");};
ref.message();} } //End of TestClient
05/05/2025
11
New Features in Java
Lambda Expressions and collections

List<String> names= new ArrayList();


names.add("jack"); names.add("ami"); names.add("bhalchadra");

Collections.sort(names , new Comparator<String>(){


public int compare(String s1 , String s2) {
return s2.compareTo(s1);}});

for(String name : names) before


{ System.out.println(name); } Java 8

Collections.sort(names,(s1,s2)->s2.compareTo(s1));
names.forEach(name->System.out.println(name));

After Java
8
05/05/2025
12
Thank you!!

13

You might also like