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

前端 未结 3 1235
既然无缘
既然无缘 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:31

    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 (update: Guava) 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."

提交回复
热议问题