title: Meaning and usage of Predicates Java 8
body: <p>I was learning the concepts of functional interfaces, lambda expression and predicates. I could write a program using examples on the internet but I am still not clear with certain constructs. </p>
  
  <p>This is my class <code>Employee</code> with 3 data members, a constructor and corresponding setters &amp; getters.</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>This is another class that has got 3 methods to check:</p>
  
  <p>a. whether the employee is a male. 
  b. whether the employee is a female.
  c. whether the age of the employee is greater than the age passed. </p>
  
  <pre><code>package lambdaandrelated;
  
  import java.util.function.Predicate;
  
      public class EmployeePredicates {
  
          public static Predicate&lt;Employee&gt; isMale(){
              return emp -&gt; emp.getGender().equalsIgnoreCase("Male");  - **Line 1**
          }
  
          public static Predicate&lt;Employee&gt; isFemale(){
              return emp -&gt; emp.getGender().equalsIgnoreCase("Female");
          }
  
          public Predicate&lt;Employee&gt; isGreaterThan(Integer age){
              return emp -&gt; emp.getAge()&gt;age;
          }
      }
  </code></pre>
  
  <p>This is my main class: </p>
  
  <pre><code>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&lt;Employee&gt; 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**
  
  
  
      }
  }
  </code></pre>
  
  <p>My doubts : </p>
  
  <p>1) Is Line 1 the implementation of <code>test()</code> method of <code>Predicate</code> interface? If yes, why is the return type of method <code>isMale()</code> a <code>Predicate&lt;Employee&gt;</code> and not a <code>boolean</code>?</p>
  
  <p>2) The o/p of Line 2 i.e the value of variable 'predicate1' is " lambdaandrelated.EmployeePredicates$$Lambda$1/746292446@4617c264 ". But the variable 'predicate1' is of type <code>Predicate&lt;Employee&gt;</code>. What is happening in the background here?</p>
  
  <p>3) What do we mean by negation of a <code>Predicate</code>? If it means negating the <code>boolean</code> o/p, then should it not be applied on the o/p of <code>test()</code> method rather than applying it on the predicate itself (as done in Line3). If it means negating the <code>Predicate</code> object, then why and how is the o/p of <code>test()</code> method getting negated? What happens in the background that is changing the <code>boolean</code> value from <code>true</code> to <code>false</code>. Is it the type of predicate object returned that decides the o/p of test() method? </p>
  
  <p>4) Also the o/p of Line4 is "java.util.function.Predicate$$Lambda$2/2055281021@5ca881b5" when the return type of <code>negate()</code> is also <code>Predicate&lt;T&gt;</code> which seems fine. Then why the o/p of <code>isMale()</code>/<code>isFemale()</code> not of the same format?</p>