Home>

I have a question about Java qualifications that I didn't understand.
Please read the following code first.

public class Sample {
    String str;
    int num;
    public Sample (String str, int num) {
    this.str = str;
    this.num = num;
    }
    public String getStr () {
        return str;
    }
    public int getNum () {
    return num;
    }
}
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Main {
// 
    public static void test (List<Sample>list, Predicate<Sample>p) {
      for (Sample s: list) {
        if (p.test (s)) {
          System.out.println (s.str);
        }
      }
    }
    public static void main (String [] args) {
        List<Sample>list = Arrays.asList (new Sample ("A", 30),
                                          new Sample ("B", 20),
                                          new Sample ("C", 10));
// 
        test (list, s->s.getNum ()>20);
    }
}


The part which is not understood is the part of in the comment.

Thinking of being a beginner (I think that there are a lot of parts that cannot be understood by those who understand it.)
public static void test (List<Sample>list, Predicate<Sample>p) {

In this code, test methods are separated by ",",List type? Sample type? List and Predicate type? Sample type? I think that there are two arguments for p, (Please continue reading m (_ _ "m))

test (list, s->s.getNum ()>20);


In this code, I thoughtlist and s had two arguments, but the () omission of the lambda expression was only when there was one argument, so my interpretation was wrong I realized that.

I checked it myself, but I don't understand and I'm in trouble.

I want to tell me

I would be happy if you could tell beginners what these two codes represent.

If there is something that cannot be explained, please comment on the supplementary request.

  • Answer # 1

      

    In this code, test methods are separated by ",", List type? Sample type? List and Predicate type? Sample type? I think p has two arguments

    Yes, there are two parameters, Listtype parameter and Predicatetype parameter p.

      

    list and s have two arguments

    No, there are two actual arguments:

    list

    s->s.getNum ()>20

    Same as writing:

    Predicatepred = s->s.getNum ()>20;
    test (list, pred);

    More specifically, it is the same as writing:

    Predicatepred = new Predicate<>() {
        @Override
        public boolean test (Sample s) {
            return s.getNum ()>20;
        }
    };
    test (list, pred);

  • Answer # 2

      

    In this code, I thought that list and s had two arguments, but I noticed that my interpretation was wrong because the () omission of lambda expression was only when there was one argument. It was.

    First, there are two arguments for this test method:list, ands->s.getNum ()>20.
    This may be easier to understand.

    public static void main (String [] args) {
            Listlist = Arrays.asList (new Sample ("A", 30),
                                              new Sample ("B", 20),
                                              new Sample ("C", 10));
            Predicatep = s->s.getNum ()>20;
            test (list, p);
        }


    First of all, make sure that lambda expressions arewritten to create objects that implement a functional interface.
    "Omitting () in lambda expression" is about a single lambda expression argument.

Trends