- java.lang.Object
-
- com.aoapps.collections.AoCollections
-
public class AoCollections extends Object
General-purpose collection utilities and constants.- Author:
- AO Industries, Inc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAoCollections.PeekIterator<E>Allows peeking the first element of iteration.
-
Field Summary
Fields Modifier and Type Field Description static SortedSet<?>EMPTY_SORTED_SET
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <E> Collection<E>asCollection(Iterable<E> iterable)Gets a collection from an iterable.static <E> List<E>asList(Iterable<E> iterable)Gets a list from an iterable.static <E> Set<E>asSet(Iterable<E> iterable)Gets a set from an iterable.static <E> SortedSet<E>asSortedSet(Iterable<E> iterable)Gets a sorted set from an iterable.static <E> Iterator<E>emptyIterator()Deprecated.UseCollections.emptyIterator()as of Java 1.7.static <T> SortedSet<T>emptySortedSet()Java 1.8: use standard versionstatic booleanequals(Collection<?> collection1, Collection<?> collection2)Two collections are considered equal when they are the same size and have the same elements in the same iteration order.static <E,R extends E>
List<R>filter(List<? extends E> list, Class<? extends R> clazz)Filters a list for all elements of a given class.static <K,V>
Set<K>filterByValue(Map<? extends K,? extends V> map, V value)Returns a modifiable set of all the keys in a map that match the given value.static <K,V>
SortedSet<K>filterByValue(SortedMap<K,? extends V> map, V value)Returns a modifiable sorted set of all the keys in a sorted map that match the given value.static intgetHashInitialCapacity(int elements)Gets the initial capacity to use for hash-based collections that will contain the given number of elements, assuming the default load factor of0.75f.static inthashCode(Iterable<?> iterable)Computes the hashCode of a collection in a manner consistent with AbstractList.static <E> ArrayList<E>newArrayList(int elements)Creates a newArrayListwith a capacity large enough to contain the given number of elements without growing.static <E> ArrayList<E>newArrayList(Integer elements)Creates a newArrayListwith a capacity large enough to contain the given number of elements without growing.static <K,V>
HashMap<K,V>newHashMap(int elements)Creates a newHashMapwith a capacity large enough to contain the given number of elements without rehashing.static <K,V>
HashMap<K,V>newHashMap(Integer elements)Creates a newHashMapwith a capacity large enough to contain the given number of elements without rehashing.static <E> HashSet<E>newHashSet(int elements)Creates a newHashSetwith a capacity large enough to contain the given number of elements without rehashing.static <E> HashSet<E>newHashSet(Integer elements)Creates a newHashSetwith a capacity large enough to contain the given number of elements without rehashing.static <K,V>
IdentityHashMap<K,V>newIdentityHashMap(int elements)Creates a newIdentityHashMapwith a capacity large enough to contain the given number of elements without rehashing.static <K,V>
IdentityHashMap<K,V>newIdentityHashMap(Integer elements)Creates a newIdentityHashMapwith a capacity large enough to contain the given number of elements without rehashing.static IntArrayListnewIntArrayList(int elements)Creates a newIntArrayListwith a capacity large enough to contain the given number of elements without growing.static IntArrayListnewIntArrayList(Integer elements)Creates a newIntArrayListwith a capacity large enough to contain the given number of elements without growing.static <K,V>
LinkedHashMap<K,V>newLinkedHashMap(int elements)Creates a newLinkedHashMapwith a capacity large enough to contain the given number of elements without rehashing.static <K,V>
LinkedHashMap<K,V>newLinkedHashMap(Integer elements)Creates a newLinkedHashMapwith a capacity large enough to contain the given number of elements without rehashing.static <E> LinkedHashSet<E>newLinkedHashSet(int elements)Creates a newLinkedHashSetwith a capacity large enough to contain the given number of elements without rehashing.static <E> LinkedHashSet<E>newLinkedHashSet(Integer elements)Creates a newLinkedHashSetwith a capacity large enough to contain the given number of elements without rehashing.static LongArrayListnewLongArrayList(int elements)Creates a newLongArrayListwith a capacity large enough to contain the given number of elements without growing.static LongArrayListnewLongArrayList(Integer elements)Creates a newLongArrayListwith a capacity large enough to contain the given number of elements without growing.static <E> SortedArrayList<E>newSortedArrayList(int elements)Creates a newSortedArrayListwith a capacity large enough to contain the given number of elements without growing.static <E> SortedArrayList<E>newSortedArrayList(Integer elements)Creates a newSortedArrayListwith a capacity large enough to contain the given number of elements without growing.static <T> Collection<T>optimalUnmodifiableCollection(Collection<? extends T> collection)Gets the optimal implementation for unmodifiable collection.static <T> List<T>optimalUnmodifiableList(List<? extends T> list)Gets the optimal implementation for unmodifiable list.static <K,V>
Map<K,V>optimalUnmodifiableMap(Map<? extends K,? extends V> map)Gets the optimal implementation for unmodifiable map.static <T> Set<T>optimalUnmodifiableSet(Set<? extends T> set)Gets the optimal implementation for unmodifiable set.static <K,V>
SortedMap<K,V>optimalUnmodifiableSortedMap(SortedMap<K,? extends V> sortedMap)Gets the optimal implementation for unmodifiable sorted map.static <T> SortedSet<T>optimalUnmodifiableSortedSet(SortedSet<T> sortedSet)Gets the optimal implementation for unmodifiable sorted set.static <E> AoCollections.PeekIterator<E>peekIterator(Iterator<? extends E> iter)Wraps the provided iterator, allowing peek of first element.static <E> Iterator<E>singletonIterator(E value)Deprecated.Please useSingletonIteratorfrom Apache Commons Collections.static <T> SortedSet<T>singletonSortedSet(T o)static <T> Collection<T>unmodifiableCopyCollection(Iterable<? extends T> iter)Performs defensive shallow copy and returns unmodifiable collection.static <T> Collection<T>unmodifiableCopyCollection(Collection<? extends T> collection)Performs defensive shallow copy and returns unmodifiable collection.static <T> List<T>unmodifiableCopyList(Iterable<? extends T> iter)Performs defensive shallow copy and returns unmodifiable list.static <T> List<T>unmodifiableCopyList(Collection<? extends T> collection)Performs defensive shallow copy and returns unmodifiable list.static <K,V>
Map<K,V>unmodifiableCopyMap(Map<? extends K,? extends V> map)Performs defensive shallow copy and returns unmodifiable map.static <T> Set<T>unmodifiableCopySet(Iterable<? extends T> iter)Performs defensive shallow copy and returns unmodifiable set.static <T> Set<T>unmodifiableCopySet(Collection<? extends T> collection)Performs defensive shallow copy and returns unmodifiable set.static <K,V>
SortedMap<K,V>unmodifiableCopySortedMap(Map<K,? extends V> map)Performs defensive shallow copy and returns unmodifiable sorted map.static <T> SortedSet<T>unmodifiableCopySortedSet(Iterable<? extends T> iter)Performs defensive shallow copy and returns unmodifiable sorted set.static <T> SortedSet<T>unmodifiableCopySortedSet(Collection<? extends T> collection)Performs defensive shallow copy and returns unmodifiable sorted set.static <E> Iterator<E>unmodifiableIterator(Iterator<? extends E> iter)Wraps an iterator to make it unmodifiable.
-
-
-
Field Detail
-
EMPTY_SORTED_SET
public static final SortedSet<?> EMPTY_SORTED_SET
-
-
Method Detail
-
emptySortedSet
public static final <T> SortedSet<T> emptySortedSet()
Java 1.8: use standard version
-
singletonSortedSet
public static <T> SortedSet<T> singletonSortedSet(T o)
-
optimalUnmodifiableCollection
public static <T> Collection<T> optimalUnmodifiableCollection(Collection<? extends T> collection)
Gets the optimal implementation for unmodifiable collection. If the collection is already unmodifiable, returns the same collection. If collection is empty, usesCollections.emptyList. If collection has one element, usesCollections.singletonList. Otherwise, wraps the collection withCollections.unmodifiableCollection.- Parameters:
collection- may benull, which will returnnull
-
asCollection
public static <E> Collection<E> asCollection(Iterable<E> iterable)
Gets a collection from an iterable. Casts the iterable to collection, if possible. Otherwise builds a new list from the iterable, maintaining iteration order.- Parameters:
iterable- may benull, which will returnnull
-
unmodifiableCopyCollection
public static <T> Collection<T> unmodifiableCopyCollection(Collection<? extends T> collection)
Performs defensive shallow copy and returns unmodifiable collection.- Parameters:
collection- may benull, which will returnnull
-
unmodifiableCopyCollection
public static <T> Collection<T> unmodifiableCopyCollection(Iterable<? extends T> iter)
Performs defensive shallow copy and returns unmodifiable collection.- Parameters:
iter- may benull, which will returnnull
-
optimalUnmodifiableList
public static <T> List<T> optimalUnmodifiableList(List<? extends T> list)
Gets the optimal implementation for unmodifiable list. If list is empty, usesCollections.emptyList. If list has one element, usesCollections.singletonList. Otherwise, wraps the list withCollections.unmodifiableList, and will also call "trimToSize" if the list is an ArrayList.- Parameters:
list- may benull, which will returnnull- See Also:
Collections.emptyList(),Collections.singletonList(java.lang.Object),ArrayList.trimToSize(),Collections.unmodifiableList(java.util.List)
-
asList
public static <E> List<E> asList(Iterable<E> iterable)
Gets a list from an iterable. Casts the iterable to list, if possible. Otherwise builds a new list from the iterable, maintaining iteration order.- Parameters:
iterable- may benull, which will returnnull
-
unmodifiableCopyList
public static <T> List<T> unmodifiableCopyList(Collection<? extends T> collection)
Performs defensive shallow copy and returns unmodifiable list.- Parameters:
collection- may benull, which will returnnull
-
unmodifiableCopyList
public static <T> List<T> unmodifiableCopyList(Iterable<? extends T> iter)
Performs defensive shallow copy and returns unmodifiable list.- Parameters:
iter- may benull, which will returnnull
-
optimalUnmodifiableSet
public static <T> Set<T> optimalUnmodifiableSet(Set<? extends T> set)
Gets the optimal implementation for unmodifiable set. If set is empty, usesCollections.emptySet. If set has one element, usesCollections.singleton. Otherwise, wraps the set withCollections.unmodifiableSet.- Parameters:
set- may benull, which will returnnull
-
asSet
public static <E> Set<E> asSet(Iterable<E> iterable)
Gets a set from an iterable. Casts the iterable to set, if possible. Otherwise builds a new set from the iterable, maintaining iteration order.- Parameters:
iterable- may benull, which will returnnull
-
unmodifiableCopySet
public static <T> Set<T> unmodifiableCopySet(Collection<? extends T> collection)
Performs defensive shallow copy and returns unmodifiable set. The iteration order of the original set is maintained.- Parameters:
collection- may benull, which will returnnull
-
unmodifiableCopySet
public static <T> Set<T> unmodifiableCopySet(Iterable<? extends T> iter)
Performs defensive shallow copy and returns unmodifiable set. The iteration order of the original set is maintained.- Parameters:
iter- may benull, which will returnnull
-
optimalUnmodifiableSortedSet
public static <T> SortedSet<T> optimalUnmodifiableSortedSet(SortedSet<T> sortedSet)
Gets the optimal implementation for unmodifiable sorted set. If sorted set is empty, usesemptySortedSet. If sorted set has one element, usessingletonSortedSet. Otherwise, wraps the sorted set withCollections.unmodifiableSortedSet.- Parameters:
sortedSet- may benull, which will returnnull
-
asSortedSet
public static <E> SortedSet<E> asSortedSet(Iterable<E> iterable)
Gets a sorted set from an iterable. Casts the iterable to sorted set, if possible. Otherwise builds a new sorted set from the iterable, in natural ordering.- Parameters:
iterable- may benull, which will returnnull
-
unmodifiableCopySortedSet
public static <T> SortedSet<T> unmodifiableCopySortedSet(Collection<? extends T> collection)
Performs defensive shallow copy and returns unmodifiable sorted set.- Parameters:
collection- may benull, which will returnnull
-
unmodifiableCopySortedSet
public static <T> SortedSet<T> unmodifiableCopySortedSet(Iterable<? extends T> iter)
Performs defensive shallow copy and returns unmodifiable sorted set.- Parameters:
iter- may benull, which will returnnull
-
optimalUnmodifiableMap
public static <K,V> Map<K,V> optimalUnmodifiableMap(Map<? extends K,? extends V> map)
Gets the optimal implementation for unmodifiable map. If map is empty, usesCollections.emptyMap. If map has one element, usesCollections.singletonMap. Otherwise, wraps the map withCollections.unmodifiableMap.- Parameters:
map- may benull, which will returnnull
-
unmodifiableCopyMap
public static <K,V> Map<K,V> unmodifiableCopyMap(Map<? extends K,? extends V> map)
Performs defensive shallow copy and returns unmodifiable map. The iteration order of the original set is maintained.- Parameters:
map- may benull, which will returnnull
-
optimalUnmodifiableSortedMap
public static <K,V> SortedMap<K,V> optimalUnmodifiableSortedMap(SortedMap<K,? extends V> sortedMap)
Gets the optimal implementation for unmodifiable sorted map. If sorted map is empty, usesemptySortedMap. If sorted map has one element, usessingletonSortedMap. Otherwise, wraps the sorted map withCollections.unmodifiableSortedMap.- Parameters:
sortedMap- may benull, which will returnnull
-
unmodifiableCopySortedMap
public static <K,V> SortedMap<K,V> unmodifiableCopySortedMap(Map<K,? extends V> map)
Performs defensive shallow copy and returns unmodifiable sorted map.- Parameters:
map- may benull, which will returnnull
-
emptyIterator
@Deprecated public static <E> Iterator<E> emptyIterator()
Deprecated.UseCollections.emptyIterator()as of Java 1.7.Gets the empty iterator.
-
singletonIterator
@Deprecated public static <E> Iterator<E> singletonIterator(E value)
Deprecated.Please useSingletonIteratorfrom Apache Commons Collections.Gets an unmodifiable iterator for a single object.
-
unmodifiableIterator
public static <E> Iterator<E> unmodifiableIterator(Iterator<? extends E> iter)
Wraps an iterator to make it unmodifiable.- Parameters:
iter- may benull, which will returnnull
-
peekIterator
public static <E> AoCollections.PeekIterator<E> peekIterator(Iterator<? extends E> iter)
Wraps the provided iterator, allowing peek of first element. Does not support null elements.
-
equals
public static boolean equals(Collection<?> collection1, Collection<?> collection2)
Two collections are considered equal when they are the same size and have the same elements in the same iteration order. If both collections are null they are also considered equal.
-
hashCode
public static int hashCode(Iterable<?> iterable)
Computes the hashCode of a collection in a manner consistent with AbstractList.- Parameters:
iterable- may benull, which will return0- See Also:
AbstractList.hashCode()
-
filter
public static <E,R extends E> List<R> filter(List<? extends E> list, Class<? extends R> clazz)
Filters a list for all elements of a given class.- Parameters:
list- may benull, which will returnnull
-
filterByValue
public static <K,V> Set<K> filterByValue(Map<? extends K,? extends V> map, V value)
Returns a modifiable set of all the keys in a map that match the given value. This is a copy of the keys and will not write-through or be altered by the original map. The set will have the same iteration order as the original map.- Parameters:
map- may benull, which will returnnull
-
filterByValue
public static <K,V> SortedSet<K> filterByValue(SortedMap<K,? extends V> map, V value)
Returns a modifiable sorted set of all the keys in a sorted map that match the given value. This is a copy of the keys and will not write-through or be altered by the original map. The set uses the same comparator as the original map.- Parameters:
map- may benull, which will returnnull
-
getHashInitialCapacity
public static int getHashInitialCapacity(int elements)
Gets the initial capacity to use for hash-based collections that will contain the given number of elements, assuming the default load factor of0.75f.- See Also:
HashMap,HashSet,IdentityHashMap
-
newArrayList
public static <E> ArrayList<E> newArrayList(int elements)
Creates a newArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements or-1when unknown
-
newArrayList
public static <E> ArrayList<E> newArrayList(Integer elements)
Creates a newArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newHashMap
public static <K,V> HashMap<K,V> newHashMap(int elements)
Creates a newHashMapwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements or-1when unknown
-
newHashMap
public static <K,V> HashMap<K,V> newHashMap(Integer elements)
Creates a newHashMapwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newHashSet
public static <E> HashSet<E> newHashSet(int elements)
Creates a newHashSetwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements or-1when unknown
-
newHashSet
public static <E> HashSet<E> newHashSet(Integer elements)
Creates a newHashSetwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newIdentityHashMap
public static <K,V> IdentityHashMap<K,V> newIdentityHashMap(int elements)
Creates a newIdentityHashMapwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements or-1when unknown
-
newIdentityHashMap
public static <K,V> IdentityHashMap<K,V> newIdentityHashMap(Integer elements)
Creates a newIdentityHashMapwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newIntArrayList
public static IntArrayList newIntArrayList(int elements)
Creates a newIntArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements or-1when unknown
-
newIntArrayList
public static IntArrayList newIntArrayList(Integer elements)
Creates a newIntArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newLinkedHashMap
public static <K,V> LinkedHashMap<K,V> newLinkedHashMap(int elements)
Creates a newLinkedHashMapwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements or-1when unknown
-
newLinkedHashMap
public static <K,V> LinkedHashMap<K,V> newLinkedHashMap(Integer elements)
Creates a newLinkedHashMapwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newLinkedHashSet
public static <E> LinkedHashSet<E> newLinkedHashSet(int elements)
Creates a newLinkedHashSetwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements or-1when unknown
-
newLinkedHashSet
public static <E> LinkedHashSet<E> newLinkedHashSet(Integer elements)
Creates a newLinkedHashSetwith a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newLongArrayList
public static LongArrayList newLongArrayList(int elements)
Creates a newLongArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements or-1when unknown
-
newLongArrayList
public static LongArrayList newLongArrayList(Integer elements)
Creates a newLongArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements ornullwhen unknown
-
newSortedArrayList
public static <E> SortedArrayList<E> newSortedArrayList(int elements)
Creates a newSortedArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements or-1when unknown
-
newSortedArrayList
public static <E> SortedArrayList<E> newSortedArrayList(Integer elements)
Creates a newSortedArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements ornullwhen unknown
-
-