What lambda expression syntax is this in Java 8?

This is one question about lambda expression. I am puzzled by the syntax in the line check((h, l) -> h > l, 0);:

The function check()requires a Climb and int object. The line above does not provide a Climb object. What does it mean h > l, 0?

interface Climb {
  boolean isTooHigh(int height, int limit);
}

class Climber {
  public static void main(String[] args) {
    check((h, l) -> h > l, 0);
  }
  private static void check(Climb climb, int height) {
    if (climb.isTooHigh(height, 1)) 
      System.out.println("too high");
    else 
      System.out.println("ok");
  }
}
+4
source share
3 answers

Your Climbinterface conforms to a functional interface contract , that is, an interface with one abstract method.

Therefore, an instance Climbcan be implemented using a lambda expression, that is, an expression that takes two ints as parameters and returns a boolean value in this case.

(h, l) -> h > l - , . h l - (int), h > l ( boolean). , , :

Climb climb = (h, l) -> h > l;
System.out.println(climb.isTooHigh(0, 2)); //false because 0 > 2 is not true
+5

-, (h, l) -> h > l , , 0 - check, ; 0 - .

+2

(h, l) -> h > l - . , true, (h) (l). 0 - int, .

+1

Source: https://habr.com/ru/post/1617021/


All Articles