The Meaning and Use of Predicates Java 8

I studied the concepts of functional interfaces, lambda expressions, and predicates. I could write a program using examples on the Internet, but I still do not understand some constructs.

This is my class Employeewith three data members, a constructor, and corresponding setters and getters.

package lambdaandrelated;

public class Employee {

    private String name;

    private String gender;

    private int age;

    public Employee(String name, String gender, int age) {
        super();
        this.name = name;
        this.gender = gender;
        this.age = age;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

This is another class that has 3 methods for checking:

and. whether the employee is male. b. whether the employee is a woman. with. Does the employee’s age depend on age?

package lambdaandrelated;

import java.util.function.Predicate;

    public class EmployeePredicates {

        public static Predicate<Employee> isMale(){
            return emp -> emp.getGender().equalsIgnoreCase("Male");  - **Line 1**
        }

        public static Predicate<Employee> isFemale(){
            return emp -> emp.getGender().equalsIgnoreCase("Female");
        }

        public Predicate<Employee> isGreaterThan(Integer age){
            return emp -> emp.getAge()>age;
        }
    }

This is my main class:

package lambdaandrelated;

import java.util.function.Predicate;

public class EmployeeMain {

    public static void main(String[] args) {
        Employee emp1 = new Employee("Parul", "Female", 24);
        Employee emp2 = new Employee("Kanika", "Female", 24);
        Employee emp3 = new Employee("Sumit", "Male", 27);


        Predicate<Employee> predicate1 = new EmployeePredicates().isGreaterThan(23);
        System.out.println(predicate1);  **Line2**
        boolean value = predicate1.test(emp3);
        System.out.println(value);

        boolean value1 = predicate1.negate().test(emp3);  **Line3**
        System.out.println(value1);

        System.out.println(predicate1.negate()); **Line4**



    }
}

My doubts:

1) Is line 1 an implementation of an test()interface method Predicate? If so, why the inverse type of the method isMale()a Predicate<Employee>, and not boolean?

2) O/p 2 i. " 1" "lambdaandrelated.EmployeePredicates$$Lambda$1/746292446@4617c264". 'predicate1' Predicate<Employee>. ?

3) a Predicate? boolean o/p, o/p test(), ( Line3). Predicate, o/p test() ? , boolean true false. , o/p test()?

4) o/p Line4 "java.util.function.Predicate$$Lambda$2/2055281021@5ca881b5", negate() Predicate<T>, . o/p isMale()/isFemale() ?

+4
3
  • isMale() - , Predicate<Employee>, , , Employee a boolean.

    Employee. , Employee .

  • , , , (, , , toString), . Predicate<Employee> predicate1 = new ClassThatImplementsPrdicateEmployee(), predicate1 ClassThatImplementsPrdicateEmployee, Predicate. , - , , .

  • , :

    default Predicate<T> negate() {
        return (t) -> !test(t);
    }
    

    Predicate, Predicate test() .

+4

1:

, , :

public static Predicate<Employee> isMale() {
    return new Predicate<Employee>() {
        @Override
        public boolean test(Employee emp) {
            return emp.getGender().equalsIgnoreCase("Male");
        }
    };
}

boolean Predicate#test. :

 return emp -> emp.getGender().equalsIgnoreCase("Male");

a Predicate, , .

2

-, , Predicate.

, EmployeePredicates , ( javap):

private static boolean lambda$isFemale$0(Employee employee ){
    // your isFemale logic here
}

, Predicate, . : EmployeePredicates$$Lambda$1. , :

-Djdk.internal.lambda.dumpProxyClasses =//

, , .

3

!= Predicate ==. negate, :

default Predicate<T> negate() {
    return (t) -> !test(t);
}

, .

+4

1) , , . , Predicate. Predicate , "- > " .

2) println toString() , Predicate. Lamdas toString, , .

3) .negate() , , , . .

+1

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


All Articles