- 
                Notifications
    You must be signed in to change notification settings 
- Fork 11.1k
ApacheCommonCollectionsEquivalents
        cpovirk edited this page Aug 5, 2020 
        ·
        5 revisions
      
    | CollectionUtils | Guava | 
|---|---|
| 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) | !Collections.disjoint(coll1, coll2)(JDK) | 
| 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 alsoIterables.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 toentrySet(),forEnumeration(), etc. | 
| Map getCardinalityMap(Collection) | ImmutableMultiset.copyOf(collection) | 
| Object index(Object, int) | Iterables.get(o, index), supplemented with calls tokeySet(),forEnumeration(), etc. | 
| Object index(Object, Object) | Iterables.get(o, index), supplemented with calls toentrySet(),forEnumeration(), etc. | 
| Collection intersection(Collection, Collection) | Sets/Multisets.intersection(a, b) | 
| boolean isEmpty(Collection) | collection == null | 
| boolean isEqualCollection(Collection, Collection) | If both are Sets orMultisets, useequals(); otherwiseImmutableMultiset.copyOf(a).equals(ImmutableMultiset.copyOf(b) | 
| boolean isFull(Collection) | No equivalent--no BoundedCollectiontype. | 
| 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) | Multisets.containsOccurrences(ImmutableMultiset.copyOf(coll1), ImmutableMultiset.copyOf(coll2)) | 
| int maxSize(Collection) | No equivalent--no BoundedCollectiontype. | 
| 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 inverseListview 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(withforEnumeration()if necessary) | 
| boolean sizeIsEmpty(Object) | Collection/Map.isEmpty(),array.length == 0,Iterables/Iterators.isEmpty(withforEnumeration()if necessary) | 
| Collection subtract(Collection, Collection) | No equivalent--create an ArrayListcontainingaand then callremoveon it for each element inb. | 
| Collection synchronizedCollection(Collection) | Collections.synchronizedCollection(collection)(JDK) | 
| void transform(Collection, Transformer) | No equivalent for transforming a Collectionin 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 aCollection... aForwardingCollectioncould 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) ConsiderImmutableCollectiontypes if you want immutability. | 
- Introduction
- Basic Utilities
- Collections
- Graphs
- Caches
- Functional Idioms
- Concurrency
- Strings
- Networking
- Primitives
- Ranges
- I/O
- Hashing
- EventBus
- Math
- Reflection
- Releases
- Tips
- Glossary
- Mailing List
- Stack Overflow
- Android Overview
- Footprint of JDK/Guava data structures