java.lang.Object
com.aoapps.collections.AoCollections
General-purpose collection utilities and constants.
- Author:
- AO Industries, Inc.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Allows peeking the first element of iteration. -
Field Summary
-
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.static final <T> SortedSet
<T> Java 1.8: use standard versionstatic 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.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 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
.static int
Computes the hashCode of a collection in a manner consistent with AbstractList.static <E> ArrayList
<E> newArrayList
(int elements) Creates a newArrayList
with a capacity large enough to contain the given number of elements without growing.static <E> ArrayList
<E> newArrayList
(Integer elements) Creates a newArrayList
with a capacity large enough to contain the given number of elements without growing.static <K,
V> HashMap <K, V> newHashMap
(int elements) Creates a newHashMap
with a capacity large enough to contain the given number of elements without rehashing.static <K,
V> HashMap <K, V> newHashMap
(Integer elements) Creates a newHashMap
with a capacity large enough to contain the given number of elements without rehashing.static <E> HashSet
<E> newHashSet
(int elements) Creates a newHashSet
with a capacity large enough to contain the given number of elements without rehashing.static <E> HashSet
<E> newHashSet
(Integer elements) Creates a newHashSet
with a capacity large enough to contain the given number of elements without rehashing.static <K,
V> IdentityHashMap <K, V> newIdentityHashMap
(int elements) Creates a newIdentityHashMap
with a capacity large enough to contain the given number of elements without rehashing.static <K,
V> IdentityHashMap <K, V> newIdentityHashMap
(Integer elements) Creates a newIdentityHashMap
with a capacity large enough to contain the given number of elements without rehashing.static IntArrayList
newIntArrayList
(int elements) Creates a newIntArrayList
with a capacity large enough to contain the given number of elements without growing.static IntArrayList
newIntArrayList
(Integer elements) Creates a newIntArrayList
with a capacity large enough to contain the given number of elements without growing.static <K,
V> LinkedHashMap <K, V> newLinkedHashMap
(int elements) Creates a newLinkedHashMap
with a capacity large enough to contain the given number of elements without rehashing.static <K,
V> LinkedHashMap <K, V> newLinkedHashMap
(Integer elements) Creates a newLinkedHashMap
with a capacity large enough to contain the given number of elements without rehashing.static <E> LinkedHashSet
<E> newLinkedHashSet
(int elements) Creates a newLinkedHashSet
with a capacity large enough to contain the given number of elements without rehashing.static <E> LinkedHashSet
<E> newLinkedHashSet
(Integer elements) Creates a newLinkedHashSet
with a capacity large enough to contain the given number of elements without rehashing.static LongArrayList
newLongArrayList
(int elements) Creates a newLongArrayList
with a capacity large enough to contain the given number of elements without growing.static LongArrayList
newLongArrayList
(Integer elements) Creates a newLongArrayList
with a capacity large enough to contain the given number of elements without growing.static <E> SortedArrayList
<E> newSortedArrayList
(int elements) Creates a newSortedArrayList
with a capacity large enough to contain the given number of elements without growing.static <E> SortedArrayList
<E> newSortedArrayList
(Integer elements) Creates a newSortedArrayList
with 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 useSingletonIterator
from 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
-
-
Method Details
-
emptySortedSet
Java 1.8: use standard version -
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, usesemptySortedSet
. 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 useSingletonIterator
from 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 newArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newArrayList
Creates a newArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newHashMap
Creates a newHashMap
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newHashMap
Creates a newHashMap
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newHashSet
Creates a newHashSet
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newHashSet
Creates a newHashSet
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newIdentityHashMap
Creates a newIdentityHashMap
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newIdentityHashMap
Creates a newIdentityHashMap
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newIntArrayList
Creates a newIntArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newIntArrayList
Creates a newIntArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newLinkedHashMap
Creates a newLinkedHashMap
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newLinkedHashMap
Creates a newLinkedHashMap
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newLinkedHashSet
Creates a newLinkedHashSet
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newLinkedHashSet
Creates a newLinkedHashSet
with a capacity large enough to contain the given number of elements without rehashing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newLongArrayList
Creates a newLongArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newLongArrayList
Creates a newLongArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements ornull
when unknown
-
newSortedArrayList
Creates a newSortedArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements or-1
when unknown
-
newSortedArrayList
Creates a newSortedArrayList
with a capacity large enough to contain the given number of elements without growing.- Parameters:
elements
- The number of elements ornull
when unknown
-
Collections.emptyIterator()
as of Java 1.7.