return true if some element of the input list passes the pred.test method but not all elements pass the test. - Java Lambda Stream

Java examples for Lambda Stream:Predicate

Description

return true if some element of the input list passes the pred.test method but not all elements pass the test.

Demo Code

import java.util.List;
import java.util.function.Predicate;

public class Main {
  /**/* w  ww.  j av a 2s .  c  o m*/
   * Implement the NoneMatch method which will return true if some element of the
   * input list passes the pred.test method but not all elements pass the test.
   * 
   * @param list
   *          : list of element to be test
   * @param pred
   *          : the predicate
   * @return true iff some elements of list pass the predicate test but some not.
   */
  public static <T> boolean someButNotAllMatch(List<T> list, Predicate<? super T> pred) {

    int r = 0, f = 0;

    for (T c : list) {
      if (pred.test(c))
        r++;
      else
        f++;
      if (r > 0 && f > 0)
        return true;
    }
    return false;
  }

}

Related Tutorials