My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ApacheCommonCollectionsEquivalents  
Know how to do something in Common Collections but not in Guava?
Updated Mar 20, 2012 by wasserman.louis

CollectionUtils (source)

void *addAll*(Collection, Enumeration)
Iterators.addAll(collection, Iterators.forEnumeration(enumeration))
void *addAll*(Collection, Iterator)
Iterators.addAll(collection, iterator)
void *addAll*(Collection, Object[])
Collections.addAll(collection, array) (JDK)
boolean *addIgnoreNull*(Collection, Object)
if (o != null) { collection.add(o); }
int *cardinality*(Object, Collection)
Iterables.frequency(collection, object)
Collection *collect*(Collection, Transformer)
newArrayList(Collections2.transform(input, function))
Collection *collect*(Collection, Transformer, Collection)
output.addAll(Collections2.transform(input, function))
Collection *collect*(Iterator, Transformer)
newArrayList(Iterators.transform(input, function))
Collection *collect*(Iterator, Transformer, Collection)
Iterators.addAll(output, Iterators.transform(input, function))
boolean *containsAny*(Collection coll1, Collection coll2)
!Sets.intersection(...).isEmpty() is an option if you have Set inputs. If not, try Iterables.any(a, Predicates.in(b)).
int *countMatches*(Collection, Predicate)
Iterables.size(Iterables.filter(collection, predicate))
Collection *disjunction*(Collection, Collection)
Sets.symmetricDifference(set1, set2)
boolean *exists*(Collection, Predicate)
Iterables.any(collection, predicate)
void *filter*(Collection, Predicate)
Iterables.removeIf(collection, not(predicate)) (see also Iterables.transform, which creates a view instead of mutating the input)
Object *find*(Collection, Predicate)
Iterables.find(collection, predicate)
void *forAllDo*(Collection, Closure)
for (Object o : collection) { closure.execute(o); }
Object *get*(Object, int)
Iterables.get(o, index), supplemented with calls to entrySet(), forEnumeration(), etc.
Map *getCardinalityMap*(Collection)
ImmutableMultiset.copyOf(collection)
Object *index*(Object, int)
Iterables.get(o, index), supplemented with calls to keySet(), forEnumeration(), etc.
Object *index*(Object, Object)
Iterables.get(o, index), supplemented with calls to entrySet(), forEnumeration(), etc.
Collection *intersection*(Collection, Collection)
Sets/Multisets.intersection(a, b)
boolean *isEmpty*(Collection)
collection == null || collection.isEmpty()
boolean *isEqualCollection*(Collection, Collection)
If both are Sets or Multisets, use equals(); otherwise ImmutableMultiset.copyOf(a).equals(ImmutableMultiset.copyOf(b)
boolean *isFull*(Collection)
No equivalent--no BoundedCollection type.
boolean *isNotEmpty*(Collection)
collection != null && !collection.isEmpty()
boolean *isProperSubCollection*(Collection, Collection)
No equivalent--check that a.size() < b.size() and then use the check described below.
boolean *isSubCollection*(Collection, Collection)
No equivalent--for two Sets, b.containsAll(a). Otherwise, create an ImmutableMultiset copy of each collection and check that the count of each element in a is <= the count of the same element in b.
int *maxSize*(Collection)
No equivalent--no BoundedCollection type.
Collection *predicatedCollection*(Collection, Predicate)
Constraints.constrainedCollection/List/Set/etc.
Collection *removeAll*(Collection, Collection)
newArrayList(Iterables.filter(collection, Predicates.not(Predicates.in(remove))))
Collection *retainAll*(Collection, Collection)
newArrayList(Iterables.filter(collection, Predicates.in(retain)))
void *reverseArray*(Object[])
Lists.reverse(Arrays.asList(array)) (returns an inverse List view without modifying array)
Collection *select*(Collection, Predicate)
newArrayList(Iterables.filter(collection, predicate))
void *select*(Collection, Predicate, Collection)
Iterables.addAll(output, Iterables.filter(input, predicate))
Collection *selectRejected*(Collection, Predicate)
newArrayList(Iterables.filter(collection, Predicates.not(predicate)))
void *selectRejected*(Collection, Predicate, Collection)
Iterables.addAll(output, Iterables.filter(input, Predicates.not(predicate)))
int *size*(Object)
Collection/Map.size(), array.length, Iterables/Iterators.size (with forEnumeration() if necessary)
boolean *sizeIsEmpty*(Object)
Collection/Map.isEmpty(), array.length == 0, Iterables/Iterators.isEmpty (with forEnumeration() if necessary)
Collection *subtract*(Collection, Collection)
No equivalent--create an ArrayList containing a and then call remove on it for each element in b.
Collection *synchronizedCollection*(Collection)
Collections.synchronizedCollection(collection) (JDK)
void *transform*(Collection, Transformer)
No equivalent for transforming a Collection in place... not very useful. Prefer transformed views (Lists/Collections2.transform) or copies of them.
Collection *transformedCollection*(Collection, Transformer)
No equivalent for transforming Objects that are added to a Collection... a ForwardingCollection could easily handle this, though.
Collection *typedCollection*(Collection, Class)
Collections.checkedCollection/Set/List/etc. (JDK)
Collection *union*(Collection, Collection)
Sets.union(a, b)
Collection *unmodifiableCollection*(Collection)
Collections.unmodifiableCollection/Set/List/etc. (JDK) Consider ImmutableCollection types if you want immutability.

Additions

I can't find a way to make the Wiki world-writable, so post any additions as comments. I'll check for new ones every so often and approve their authors as Wiki editors.

Comment by project member jr...@google.com, Apr 19, 2011
Comment by project member tv@duh.org, Apr 19, 2011

In various places here, newArrayList() should be qualified (Lists.newArrayList) for newcomers. It's a valuable method that saves redundant generics syntax.

Collection subtract(Collection, Collection) version of English comment?: Lists.newArrayList(a).removeAll(b)

Comment by project member cgdec...@gmail.com, Apr 19, 2011

@tv@duh.org: Unfortunately, Lists.newArrayList(a).removeAll(b) is not equivalent to "create an ArrayList? containing a and then call remove on it for each element in b". For example, if A is a List containing [1, 1, 1, 1] and B is a List containing 1?, A.removeAll(B) would result in A being empty. Iterating through B and calling remove for each element would result in A containing [1, 1, 1] which is equivalent to the behavior of the subtract method.

Comment by project member michael....@gmail.com, Aug 16, 2011

AbstractMapDecorator? => ForwardingMap?

Comment by project member wasserman.louis, Oct 17, 2011

boolean containsAny(Collection coll1, Collection coll2) can be done simply as

!java.util.Collections.disjoint(coll1, coll2)

Comment by knightof...@gmail.com, Oct 26, 2011

This page has saved me so much time. I only wish I had found it sooner. I'm refactoring my current project to use Guava in place of Apache Commons.

Comment by jso...@excilys.com, Dec 16, 2011

Object find(Collection, Predicate) without exception if no match

Iterables.find(collection, predicate,null)

Sign in to add a comment
Powered by Google Project Hosting