views:

878

answers:

4

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

It feels smelly to have to write a foreach loop to inspect the return of a method on each object, so I'm hoping there's a better Java idiom for it.

+8  A: 

Predicates are provided in the Google Collections library.

erickson
A: 

As far as I know, no. But Apache Commons Collections has something like this: Predicate


Edit: Right, as noted in comments, Commons Collections is from pre-generics world, so Google Collections seems like a clearly better option now. Still, Commons Collections deserves to be mentioned as it's a well-known library that does this, and also so that people know why not to use it. :)

I was just reading more about Google Collections in this nice interview with its main developers, and wanted to quote a bit that deals specifically with the "Google Collections vs. Apache Commons Collections" issue:

What is unique about your approach? How does it differ to, for example, the Apache Commons Collection?

Kevin: "Well, thank God for the Apache Commons. We'd all be in bad shape without libraries like this. That said, sadly that particular project has stalled, in a pre-generics world. They do want to adopt generics, but they recognize that this would involve a pretty nontrivial and incompatible rewrite. So far, no one seems to be actively driving such an effort. At Google we've been using Java 5 company-wide since the spring of 2005. A collections library being ungenerified was a deal-breaker for us, because we really hate getting compiler warnings. I was also concerned about the many places in which the Apache collections don't conform to the specifications of the JDK interfaces they implement."

[...]

Jared: "As Kevin implies, our library is the only collections library I know of, outside the JDK, built with Java 5 features: generics, enums, covariant return types, etc. When writing Java 5 code, you want a collections library that takes full advantage of the language. In addition, we put enormous effort into making the library complete, robust, and consistent with the JDK collection classes. Our collection classes were much more limited initially, but we've gradually improved them over the last two years. Since all library usage is in Google's source control system, we've had the flexibility to modify public interfaces. An open-source project like Apache Commons Collection doesn't have the freedom to change its behavior after the initial release. Since we'll lose that flexibility once Google Collections Library 1.0 is released, we're eager to receive feedback now so we can get things right."

Jonik
Commons Collections lacks generic support.
erickson
A: 

You can´t compare .Net Predicate with the one provided by Google Collections and Apache Commons Collection.

.Net predicates are cool because they involve some language features that are now found in Java language.

Of course you can simulate that in Java using anon classes and interfaces, but it is not the same thing :)

razenha
+2  A: 

Functional Java provides first-class functions. A predicate is expressed as F<T, Boolean>. 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<String> a = array("Hello", "There", "how", "ARE", "yOU?");  
    final boolean b = a.exists(matches.f("^[a-z]*$"));  
    System.out.println(b); // true
  }  
}
Apocalisp