Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a viable generic alternative to apache.commons.collections.CollectionUtils?

Is there a viable generic version of org.apache.commons.collections.CollectionUtils? If not, why not? It seems like an obvious need. Or has the Java community just given up on functional coding until closures are added to Java 17?

like image 737
kevin cline Avatar asked Aug 10 '11 18:08

kevin cline


People also ask

What is the use of CollectionUtils in Java?

Simply put, the Apache CollectionUtils provides utility methods for common operations which cover a wide range of use cases and helps in avoiding writing boilerplate code. The library targets older JVM releases because currently, similar functionality is provided by the Java 8's Stream API.

What is Apache Commons collections used for?

Apache Commons Collections - Overview Commons Collections augments Java Collections Framework. It provides several features to make collection handling easy. It provides many new interfaces, implementations and utilities.

What is Apache Commons Collections library?

The Apache Commons Collections are the components of the Apache Commons which are derived from Java API and provides component architecture for the Java language. Commons-Collections seek to build upon the JDK classes by providing new interfaces, implementations and utilities.

What does CollectionUtils isNotEmpty do?

isNotEmpty() method of CollectionUtils can be used to check if a list is not empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list.


2 Answers

There's a genericified port of a slightly out-of-date version of Commons Collections here. It's no longer maintained, however.

A better option is Google Guava. It has classes like Lists, Sets, Collections2 etc that are the equivalent to Commons' CollectionUtils. Guava is not a one-for-one port of Commons Collections, but it has a better designed and more consistent API. Takes a bit of adjusting to, if you're used to Commons Collections.

like image 152
skaffman Avatar answered Oct 14 '22 10:10

skaffman


Looks like situation around Apache Commons Collections has changed. API version 4.0 is strongly typed. It is already available in Maven Central Repository.

like image 38
JeanVEGA Avatar answered Oct 14 '22 10:10

JeanVEGA