Parameter(s) -> body of expression
Zero Parameters - No parameters are passed.
( ) -> System.println("No Parameters")
One Parameter - Only one parameter is passed.
(x) -> System.println("One Parameter: " + x)
Multiple Parameters - Multiple parameters are passed.
(x,y) -> System.println("Multiple Parameters: " + x + " " + y);
Accepts two values x, y and returns the product of those two numbers.
(x, y) -> x * y
Examples: FileFilter
, Comparators
, Runnable
etc.
import java.util.*;
// Functional Interface
interface Addition {
void answer(int x, int y); // Abstract function
}
public class Main {
public static void main(String args[]) {
Addition obj = (int x, int y) - > System.out.println(x + y);
obj.answer(25, 25);
}
}
Output:
50
Lambda expressions can access variables declared outside the lambda function under certain conditions.
import java.io.*;
interface abst {
void local(String s);
}
class main {
public static void main(String[] args) {
String text1 = "bcbcc"; // Local Variable
abst obj = (String a) - > System.out.println(a);
obj.local(text1);
}
}
Output:
bcbcc
File: Lambda.java
import java.io.*;
interface Interf {
void print();
}
public class Lambda {
int x; // instance variable
static int y = 100; // static variable
Lambda(int x) {
this.x = x;
}
void show() {
Interf test = () - > {
System.out.println("Value of x = " + x);
System.out.println("Value of y = " + y);
};
test.print();
}
public static void main(String arg[]) {
Lambda obj = new Lambda(50);
obj.show();
}
}
Output:
$ javac Lamda.java
$ java Lamda
Value of x = 50
Value of y = 100
Method reference is used to give reference to methods in the functional interface. It makes the code more concise and much more reading friendly.
import java.io.*;
interface abst {
void local(int x, int y);
}
public class Test {
public static void adding(int x, int y) {
System.out.println("Sum = " + (x + y));
}
}
class Example {
public static void main(String[] args) {
abst ref = Test::adding;
ref.local(30, 30);
//output - Sum = 60
}
}
import java.io.*;
interface abst {
void local(int x, int y);
}
public class Test {
public void adding(int x, int y) {
System.out.println("Sum = " + (x + y));
}
}
class Example {
public static void main(String[] args) {
Test add = new Test();
abst ref = add::adding;
ref.local(30, 30);
}
}
File : Test.java
import java.io.*;
interface Interf {
Hello getMessage(String s);
}
class Hello {
Hello(String s) {
System.out.println(s);
}
}
public class Test {
public static void main(String[] args) {
Interf obj = Hello::new;
obj.getMessage("Hello World");
}
}
Output:
$ javac Test.java
$ java Test
Hello World
import java.util.*;
public class Example {
public static void main(String[] args) {
List < Integer > list = new ArrayList < Integer > ();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.forEach((n) - > System.out.println(n));
}
}
Output:
2
3
4
5
import java.util.*;
import java.util.stream.Stream;
class Product{
String brand;
float price;
public Product(String brand, float price) {
super();
this.brand = brand;
this.price = price;
}
}
public class Test {
public static void main(String[] args)
{
List<Product> lst=new ArrayList<Product>();
lst.add(new Product("Asus",10000));
lst.add(new Product("Dell",20000));
lst.add(new Product("Rogue",30000));
lst.add(new Product("Mac Book",40000));
Stream<Product> data = lst.stream().filter(p -> p.price > 15000);
data.forEach( product -> System.out.println(product.brand+": "+product.price));
}
}
Output:
$ javac Test.java
$ java Test
Dell: 20000.0
Rogue: 30000.0
Mac Book: 40000.0
Help us improve this content by editing this page on GitHub