Sunday, 9 August 2015

Predicate interface

It is just like Function interface and has two methods.
public interface Predicate<T> {
  boolean apply(T input)
  boolean equals(Object object)
}

The apply method returns the result of applying Predicate to the input.

import com.google.common.base.Predicate;

public class PredicateEx {

  static Predicate<String> userNameLength = new Predicate<String>() {

    @Override
    public boolean apply(String input) {
      if (input.length() > 6)
        return true;
      return false;
    }

  };

  public static void main(String args[]) {
    System.out.println(userNameLength.apply("krish"));
    System.out.println(userNameLength.apply("krishna"));
    System.out.println(userNameLength.apply("Arjun"));
  }
}


Output

false
true
false



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment