java.lang.Object
com.aoapps.collections.AoCollections
General-purpose collection utilities and constants.
- Author:
- AO Industries, Inc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAllows peeking the first element of iteration. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final SortedSet<?> Deprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionstatic <E> Collection<E> asCollection(Iterable<E> iterable) Gets a collection from an iterable.static <E> List<E> Gets a list from an iterable.static <E> Set<E> 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> Deprecated.UseCollections.emptyIterator()as of Java 1.7.static <T> SortedSet<T> Deprecated, for removal: This API element is subject to removal in a future version.Please useCollections.emptySortedSet().static 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> 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 intComputes 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 Details
-
EMPTY_SORTED_SET
Deprecated, for removal: This API element is subject to removal in a future version.Please useCollections.emptySortedSet().
-
-
Method Details
-
emptySortedSet
Deprecated, for removal: This API element is subject to removal in a future version.Please useCollections.emptySortedSet(). -
singletonSortedSet
-
optimalUnmodifiableCollection
Gets the optimal implementation for unmodifiable collection. If collection is empty, usesCollections.emptyList. If the collection is already unmodifiable, returns the same collection. If collection has one element, usesCollections.singletonList. Otherwise, wraps the collection withCollections.unmodifiableCollection.- Parameters:
collection- may benull, which will returnnull
-
asCollection
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
Performs defensive shallow copy and returns unmodifiable collection.- Parameters:
collection- may benull, which will returnnull
-
unmodifiableCopyCollection
Performs defensive shallow copy and returns unmodifiable collection.- Parameters:
iter- may benull, which will returnnull
-
optimalUnmodifiableList
Gets the optimal implementation for unmodifiable list. If list is empty, usesCollections.emptyList. If the list is already unmodifiable, returns the same list. 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:
-
asList
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
Performs defensive shallow copy and returns unmodifiable list.- Parameters:
collection- may benull, which will returnnull
-
unmodifiableCopyList
Performs defensive shallow copy and returns unmodifiable list.- Parameters:
iter- may benull, which will returnnull
-
optimalUnmodifiableSet
Gets the optimal implementation for unmodifiable set. If set is empty, usesCollections.emptySet. If the set is already unmodifiable, returns the same set. If set has one element, usesCollections.singleton. Otherwise, wraps the set withCollections.unmodifiableSet.- Parameters:
set- may benull, which will returnnull
-
asSet
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
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
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
Gets the optimal implementation for unmodifiable sorted set. If sorted set is empty, usesCollections.emptySortedSet(). If the sorted set is already unmodifiable, returns the same sorted set. If sorted set has one element, usessingletonSortedSet. Otherwise, wraps the sorted set withCollections.unmodifiableSortedSet.- Parameters:
sortedSet- may benull, which will returnnull
-
asSortedSet
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
Performs defensive shallow copy and returns unmodifiable sorted set.- Parameters:
collection- may benull, which will returnnull
-
unmodifiableCopySortedSet
Performs defensive shallow copy and returns unmodifiable sorted set.- Parameters:
iter- may benull, which will returnnull
-
optimalUnmodifiableMap
Gets the optimal implementation for unmodifiable map. If map is empty, usesCollections.emptyMap. If the map is already unmodifiable, returns the same map. If map has one element, usesCollections.singletonMap. Otherwise, wraps the map withCollections.unmodifiableMap.- Parameters:
map- may benull, which will returnnull
-
unmodifiableCopyMap
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 the sorted map is already unmodifiable, returns the same sorted map. If sorted map has one element, usessingletonSortedMap. Otherwise, wraps the sorted map withCollections.unmodifiableSortedMap.- Parameters:
sortedMap- may benull, which will returnnull
-
unmodifiableCopySortedMap
Performs defensive shallow copy and returns unmodifiable sorted map.- Parameters:
map- may benull, which will returnnull
-
emptyIterator
Deprecated.UseCollections.emptyIterator()as of Java 1.7.Gets the empty iterator. -
singletonIterator
Deprecated.Please useSingletonIteratorfrom Apache Commons Collections.Gets an unmodifiable iterator for a single object. -
unmodifiableIterator
Wraps an iterator to make it unmodifiable. If the iterator is already unmodifiable, returns the same iterator.- Parameters:
iter- may benull, which will returnnull
-
peekIterator
Wraps the provided iterator, allowing peek of first element. Does not support null elements. -
equals
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
Computes the hashCode of a collection in a manner consistent with AbstractList.- Parameters:
iterable- may benull, which will return0- See Also:
-
filter
Filters a list for all elements of a given class.- Parameters:
list- may benull, which will returnnull
-
filterByValue
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
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:
-
newArrayList
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Creates a newSortedArrayListwith a capacity large enough to contain the given number of elements without growing.- Parameters:
elements- The number of elements ornullwhen unknown
-

Collections.emptySortedSet().