S
S
SankaSanka2019-07-05 18:26:51
Java
SankaSanka, 2019-07-05 18:26:51

What does lambda want from me?

public class test{
  
  interface Chislo {
    public boolean chislo(int x);
  }
  
  public static void main(String[] args) {
      final int x= 10;

      Chislo a = x->{ 
      if ((x%13) == 0) return true;
      return false;
      };
      
      	System.out.println(a);
      }
      
      
  
  }

I want from him that the lambda returns true if x is divided without a remainder by 13 and vice versa.
what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
faoxy, 2019-07-07
@SankaSanka

In your case, the lambda did not work because you didn't call a.chislo(x). In general, you can do it much easier:

public static void main(String[] args) {
        Predicate<Integer> isDivided13 = x -> (x%13) == 0;
        Boolean result = isDivided13.test(10);
        System.out.println(result);
    }

Here, the first line behind main defines the behavior ( ). And the next one starts the calculation for a particular value by calling the . It is worth noting here that the interface is a standard type constructor and takes as a generic type the input value (method argument ). x -> (x%13) == 0testPredicatetest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question