cs notebook2
  • Introduction
  • functional interface
  • Lambda expressions
  • Stream
  • Method and Constructor Reference
  • Collectors
  • exe
  • Interface Changes
Powered by GitBook
On this page
  • 1. Lambda parameter
  • One parameter
  • Multiple parameter
  • Parameter Types
  • 2. Lambda Function Body
  • 3. Returning a Value From a Lambda Expression
  • 4. Lambdas as Objects

Was this helpful?

Lambda expressions

1. Lambda parameter

One parameter

param -> System.out.println("One parameter", param);

Multiple parameter

(p1, p2) -> System.out.println("Multiple parameter"+p1+" "+p2);

Parameter Types

(Car car) -> System.out.println("The car is: "+car.getName());

2. Lambda Function Body

(oldState, newState) ->{
    System.out.println("Old state: "+oldState);
    System.out.println("New state: "+newState);
}

3. Returning a Value From a Lambda Expression

//original
(a1, a2) ->{return a1>a2;}

//change into
(a1,a2) ->a1>a2;

4. Lambdas as Objects

public interface MyComparator{
    public boolean compare(int a1, int a2);
}

MyComparator myComparator=(a1, a2) -> return a1> a2;
boolean result = myComparator.compare(2,5);
Previousfunctional interfaceNextStream

Last updated 5 years ago

Was this helpful?