Is there a Java 1.5 equivalent to the Predicate methods in .Net?

前端 未结 3 1255
既然无缘
既然无缘 2021-01-12 06:11

Specifically, I\'m looking for similarly clean notation to the Collection.TrueForAll / Exists, etc.

It feels smelly to have to wri

3条回答
  •  無奈伤痛
    2021-01-12 06:38

    Functional Java provides first-class functions. A predicate is expressed as F. For example, here's a program that tests an array for the existence of a string that is all lowercase letters.

    import fj.F;  
    import fj.data.Array;  
    import static fj.data.Array.array;
    import static fj.function.Strings.matches;
    
    public final class List_exists {  
      public static void main(final String[] args) { 
        final Array a = array("Hello", "There", "how", "ARE", "yOU?");  
        final boolean b = a.exists(matches.f("^[a-z]*$"));  
        System.out.println(b); // true
      }  
    }
    

提交回复
热议问题