简体   繁体   English

Java - 使用不带lambda表达式的谓词

[英]Java - Use predicate without lambda expressions

I've below requirement:- 我有以下要求: -

Employee.java Employee.java

public boolean isAdult(Integer age) {
    if(age >= 18) {
        return true;
    }
    return false;
}

Predicate.java Predicate.java

    private Integer age;
Predicate<Integer> isAdult;

public PredicateAnotherClass(Integer age, Predicate<Integer> isAdult) {
    this.age = age;
    System.out.println("Test result is "+ isAdult(age));
}

public void testPredicate() {
    System.out.println("Calling the method defined in the manager class");

}

Now My goal is to test whether the age which i pass to Predicate is adult or not using the method defined in Employee class , for which i am passing the method reference which i pass in the constructor of Predicate class. 现在我的目标是使用Employee类中定义的方法测试我传递给Predicate年龄是否成人,我传递的方法参考是我在Predicate类的构造函数中传递的。

But i don't know how to call the method defined in Employee class, below is my test class :- 但我不知道如何调用Employee类中定义的方法,下面是我的测试类: -

public class PredicateTest {

    public static void main(String[] args) {
        PredicateManager predicateManager = new PredicateManager();

        PredicateAnotherClass predicateAnotherClass = new PredicateAnotherClass(20, predicateManager::isAdult);
        predicateAnotherClass.testPredicate();;
    }
}

I am getting the compilation error in the System.out.println("Test result is "+ isAdult(age)); 我收到System.out.println("Test result is "+ isAdult(age));的编译错误System.out.println("Test result is "+ isAdult(age)); in the predicate class. predicate类中。

Let me know how to resolve this issue. 让我知道如何解决这个问题。 and if i need to provide any other information. 如果我需要提供任何其他信息。

This looks a little bit suspicious, you care if the employee is an adult, so your method should really take a Employee as an argument and a Predicate<Employee> , like this: 这看起来有点可疑,你关心员工是否是成年人,所以你的方法应该把Employee作为参数和Predicate<Employee> ,如下所示:

 private static void testEmployee(Employee emp, Predicate<Employee> predicate) {
    boolean result = predicate.test(emp);
    System.out.println(result);
}

And the usage of this method would be: 这种方法的用法是:

testEmployee(new Employee(13), emp -> emp.isAdult(emp.getAge()));

The thing is you can reuse this method for other predicates as well, let's say you want to test gender, or income, etc. 问题是你可以将这种方法重用于其他谓词,假设你想测试性别或收入等。

Predicate interface has method test() . 谓词接口有方法test() You should use this method in a following way: 您应该以下列方式使用此方法:

isAdult.test(age)

This method evaluates this predicate on the given argument. 此方法根据给定的参数计算此谓词。 It returns true if the input argument matches the predicate, otherwise false 如果输入参数与谓词匹配,则返回true,否则返回false

Predicate has test method, that is used, when working with streams/optionals. 当使用流/选项时,Predicate具有使用的测试方法。

public PredicateAnotherClass(Integer age, Predicate<Integer> isAdultFilter) {
    this.age = age;
    System.out.println("Test result is "+ isAdultFilter.test(age));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM