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.
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 meI 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
-
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) { List
list = Arrays.asList (new Sample ("A", 30), new Sample ("B", 20), new Sample ("C", 10)); Predicate p = 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.
Related articles
- java main method not found using eclipse
- method of confirming equivalence of different types in java
- java - method using graphics class created in another file cannot be used in main method of main class
- java - send method return value to servlet with jsp
- java - i don't know how to read a file or write a file name with command line arguments
- java - to create a method and call it from another class
- i'm a newbie to java what happens if i write all the documents in java for this problem?
- java - about flatmap method
- java instance generation method
- i want to write a method for reading from standard input in ruby
- java - i don't know the cause of the error "this method must return a result of type ○○○○"
- java - i want to use the return value of one method in another
- java - about the stringvalueof () method
- java - equals method
- what happens when multiple java users call the same method
- java - intelij replacement method
- java - i want to create a method that gets and returns the name corresponding to the number
- java - i would like to know how to call the original method with the original implementation when using mockito's spy
- java - about method overloading
- java - code of error diffusion method in processing
Yes, there are two parameters, Listtype parameter and Predicatetype parameter p.
No, there are two actual arguments:
list
s->s.getNum ()>20
Same as writing:
More specifically, it is the same as writing: