0% found this document useful (0 votes)
28 views

JAVA 8 Features

Uploaded by

rahul5949
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

JAVA 8 Features

Uploaded by

rahul5949
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

JAVA 8 Features

Agenda
 Lambda Expression
 Functional Interfaces
 Predicates
 Functions
Lambda (λ) Expression
 The other languages which uses lambda expressions are:

 C#.Net
 C Objective
C
 C++
 Python
 Ruby etc.
 and finally in java also.

 The Main Objective of λ Lambda Expression is to bring benefits of functional programming into
java.
What is Lambda Expression (λ):
 Lambda Expression is just an anonymous(nameless) function. That means the function which doesn’t have the
name, return type and access modifiers.

Ex: 1 public void m1() { () ->


{ S.o.p(“hello”); }
S.o.p(“hello”);
}
Ex:2 public void add(int a, int b) { (int a, int b) () -> S.o.p(a+b);
S.o.p(a+b);
}
If the type of the parameter can be decided by compiler automatically based on the context then we can remove
types also.
The above Lambda expression we can rewrite as (a,b) -> S.o.p (a+b);

Ex: 3 public String str(String str) { (String str) -> return str;
return str; } (str) -> str;
Conclusions:
 A lambda expression can have zero or more number of parameters(arguments).
Ex: () -> S.o.p(“hello”); (int a ) -> S.o.p(a); (int a, int b) -> return a+b;

 Usually we can specify type of parameter. If the compiler expect the type based on the context then we can
remove type. i.e., programmer is not required.
Ex: (int a, int b) -> S.o.p(a+b); (a,b) -> S.o.p(a+b);

 If multiple parameters present then these parameters should be separated with comma(,).

 If zero number of parameters available, then we have to use empty parameter [ like ()]. Ex: () ->
sop(“hello”);

 If only one parameter is available and if the compiler can expect the type, then we can remove the type and
parenthesis also.
Ex: (int a) -> S.o.p(a); (a) -> S.o.p(a); a -> S.o.p(a);

 Once we write lambda expression we can call that expression just like a method, for this functional interfaces
are required.
Functional Interfaces:
 if an interface contain only one abstract method, such type of interfaces are
called functional interfaces and the method is called functional method or single
abstract method(SAM).
Ex:
1) Runnable It contains only run() method
2) Comparable It contains only compareTo() method
3) ActionListener It contains only actionPerformed()
4) Callable It contains only call() method

 Inside functional interface in addition to single Abstract method(SAM) we write any


number of default and static methods.
Ex:
interface Interf {
public abstract void m1();
default void m2() {
System.out.println (“method m2”); }
@FunctionalInterface annotation
 Java 8 introduced @FunctionalInterface annotation to specify that the interface is
Functional Interface.
Ex: @FunctionalInterface
Interface Interf {
public void m1(); } this code compiles without any
compilation errors.
 Inside Functional Interface we can take only one abstract method, if we take more than
one abstract method then compiler raise an error message, we will get compilation
error.
Ex: @FunctionalInterface
Interface Interf {
public void m1(); this code compiles gives compile time error
public void m2(); }

 Inside FunctionalInterface we have to take exactly only one abstract method.If we are
not declaring that abstract method then compiler gives an error message.
Ex: @FunctionalInterface
Interface Interf { compilation error
}
Functional Interface Vs Lambda Expressions:
Once we write Lambda expressions to invoke its functionality, then Functional Interface is
required. We can use Functional Interface reference to refer Lambda Expression. Wherever
Functional Interface concept is applicable there we can use Lambda Expressions

Ex:1 Without Lambda Expression code With Lambda expression


interface Interf {
public void methodOne() {}
interface Interf {
} public void methodOne() {}
public class Demo implements Interface { }
public void methodOne() {
System.out.println( “method one execution”); }
class Test {
public static void main(String[] args) {
public class Test {
Interf i = () ->
public static void main(String[] args) {
System.out.println(“MethodOneExecution”);
Interf i = new Demo();
i.methodOne();
i.methodOne();
} }
} }
Ex:1 Without Lambda Expression code With Lambda expression
interface Interf {
public void sum(int a, int b) {} interface Interf {
} public void sum(int a, int b) {}
public class Demo implements Interface {
public void sum(int a, int b) {
}
System.out.println( “The sum:” +(a+b)); } class Test {
class Test {
public static void main(String[] args) {
public static void main(String[] args) { Interf i = (a,b) -> System.out.println
(“The sum:” +(a+b));
Interf i = new Demo();
i. sum(20,15); i.sum(20,15);
} }
}
}
Predicates

 A predicate is a function with a single argument and returns boolean value.


 Predicate interface present in java.util.function package.
 It’s a functional interface and it contains only one method i.e., test()
interface Predicate<T> {
public boolean test(T t);
}
 As predicate is a functional interface and hence it can refers lambda
expression
Ex:1 Write a predicate to check whether the given integer is greater than 10 or not.
import java.util.function;
class Test {
public static void main(String[] args) {
predicate<Integer> p = I -> (i>10);
System.out.println(p.test(100)); // true
System.out.println(p.test(7)); //
false
System.out.println(p.test(true)); //CE
}
}
Ex: 2 Write a predicate to check the length of given string is greater than 3 or not.
Predicate<String> p = s -> (s.length() > 3);
System.out.println (p.test(“rvkb”)); // true
System.out.println (p.test(“rk”)); // false
Function

 Functions are exactly same as predicates except that functions can return any
type of result.
 Function interface also present in java.util.function package.
 Functional interface contains only one method i.e., apply()

interface function(T,R) {
public R apply(T t);
}
 Ex Write a function to find length of given input string.

import java.util.function.*;
class Test {
public static void main(String[] args) {
Function<String, Integer> f = s ->s.length();
System.out.println(f.apply(“Rahul"));

System.out.println(f.apply("Srivastava"));
}
}
Difference between predicate and function
Predicate Function
 To implement conditional checks  To perform certain operation And to
We should go for predicate return some result we Should go for
 Predicate can take one type function
Parameter which represents Input  Function can take 2 type Parameters.
argument type. Predicate<T> first one represent Input argument
 type and Second one represent return
Predicate interface defines only
Type. Function<T,R>
one method called test()
 Function interface defines only one
 public boolean test(T t) Method called apply().
 Predicate can return only boolean  public R apply(T t)
value.  Function can return any type of value
Any
Questions ?

You might also like