Professional Documents
Culture Documents
Less coding.
No Parameter Syntax
() -> {
}
(p1) -> {
(p1,p2) -> {
interface Drawable{
int width=10;
};
d.draw();
Now, we are going to implement the above example with the help
of Java lambda expression.
interface Drawable{
int width=10;
//with lambda
Drawable d2=()->{
System.out.println("Drawing "+width);
};
d2.draw();
interface Sayable{
Sayable s=()->{
};
System.out.println(s.say());
interface Sayable{
Sayable s1=(name)->{
};
System.out.println(s1.say("Sonoo"));
};
System.out.println(s2.say("Sonoo"));
interface Addable{
int add(int a,int b);
Addable ad1=(a,b)->(a+b);
System.out.println(ad1.add(10,20));
System.out.println(ad2.add(100,200));
interface Addable{
Addable ad1=(a,b)->(a+b);
System.out.println(ad1.add(10,20));
return (a+b);
};
System.out.println(ad2.add(100,200));
import java.util.*;
list.add("ankit");
list.add("mayank");
list.add("irfan");
list.add("jai");
list.forEach(
(n)->System.out.println(n)
);
There are various new features in Java 8, but the following are the most important:
1. Lambda Expressions −a new feature of the language that allows us to consider actions
as objects
to abstract methods
6. Nashorn, JavaScript Engine − JavaScript code execution and evaluation engine based
on ava.
7. Stream API −a particular iterator class that lets us to efficiently process collections of
items
8. Date API −a Date API inspired by JodaTime that is enhanced and immutable
An interface with only one abstract method is known as a functional interface. As a result,
it's also known as the SAM (Single Abstract Method) interface. Because it covers a function as
Default, static, and overridden methods can all be found in functional interfaces. The
will cause a compiler error if it is used on interfaces with more than one abstract method.
@FunctionalInterface
interface Square {
class Test {
int a = 5;
// lambda expression to define the calculate method
System.out.println(ans);
Lambda expression is a new feature which is introduced in Java 8. A lambda expression is an anonymous
function. A function that doesn’t have a name and doesn’t belong to any class. The concept of lambda
expression was first introduced in LISP programming language.
To create a lambda expression, we specify input parameters (if there are any) on the left side of the
lambda operator ->, and place the expression or block of statements on the right side of lambda
operator. For example, the lambda expression (x, y) -> x + y specifies that lambda expression takes two
arguments x and y and returns the sum of these.
1. Name
2. Parameter list
3. Body
4. return type.
2. Parameter list
4. No return type – The java 8 compiler is able to infer the return type by checking the code. you need
not to mention it explicitly.
@FunctionalInterface
interface MyFunctionalInterface {
// lambda expression
return "Hello";
};
System.out.println(msg.sayHello());
}
@FunctionalInterface
interface MyFunctionalInterface {
System.out.println(f.incrementByFive(22));
@FunctionalInterface
interface Display {
void display();
ref.display();
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);
map.put("D", 40);
map.put("E", 50);
map.put("F", 60);
1.2 In Java 8, we can use forEach to loop a Map and print out its entries.
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);
map.put("D", 40);
map.put("E", 50);
map.put("F", 60);
// lambda
Output
Terminal
Key : A, Value : 10
Key : B, Value : 20
Key : C, Value : 30
Key : D, Value : 40
Key : E, Value : 50
Key : F, Value : 60
In Java 8, the Java.lang interface now supports a “forEach” function. Iterable that can iterate
over the collection’s items. The Iterable interface has a default method called “forEach.”
Collection classes use it to iterate items, which extends the Iterable interface.You may send
Lambda Expression as an argument to the “forEach” method, which accepts the Functional
Interface as a single parameter.
Optional Class
In Java 8, the “java.util” package included an optional class. The public final class “Optional” is used to
handle NullPointerException in a Java program. You may give other code or values to execute using
Optional. Thus, optional reduces the number of null checks required to avoid a nullPointerException.
You may use the Optional class to prevent the application from crashing and terminating unexpectedly.
The Optional class has methods for checking the existence of a value for a given variable.
Optional<String>checkNull =
Optional.ofNullable(str[5]);
if (checkNull.isPresent()) {
System.out.print(str);
} else
System.out.println("string is null");