Class CollectionUtils
- Since:
- 1.1.3
- Author:
- Juergen Hoeller, Rob Harrop, Arjen Poutsma, Sam Brannen
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic List<?> arrayToList(@Nullable Object source) Convert the supplied array into a List.static <K,V> Map <K, V> compositeMap(Map<K, V> first, Map<K, V> second) Return a (partially unmodifiable) map that combines the provided two maps.static <K,V> Map <K, V> compositeMap(Map<K, V> first, Map<K, V> second, @Nullable BiFunction<K, V, V> putFunction, @Nullable Consumer<Map<K, V>> putAllFunction) Return a map that combines the provided maps.static booleanCheck whether the givenEnumerationcontains the given element.static booleanCheck whether the givenIteratorcontains the given element.static booleancontainsAny(Collection<? extends @Nullable Object> source, Collection<? extends @Nullable Object> candidates) Returntrueif any element in 'candidates' is contained in 'source'; otherwise returnsfalse.static booleancontainsInstance(@Nullable Collection<? extends @Nullable Object> collection, @Nullable Object element) Check whether the givenCollectioncontains the given element instance.findCommonElementType(Collection<?> collection) Find the common element type of the given Collection, if any.static <E> @Nullable EfindFirstMatch(Collection<?> source, Collection<E> candidates) Return the first element in 'candidates' that is contained in 'source'.static <T> @Nullable TfindValueOfType(@Nullable Collection<?> collection, @Nullable Class<T> type) Find a single value of the given type in the given Collection.findValueOfType(Collection<?> collection, Class<?>[] types) Find a single value of one of the given types in the given Collection: searching the Collection for a value of the first type, then searching for a value of the second type, etc.static <T> @Nullable TfirstElement(@Nullable List<T> list) Retrieve the first element of the given List, accessing the zero index.static <T> @Nullable TfirstElement(@Nullable Set<T> set) Retrieve the first element of the given Set, usingSortedSet.first()or otherwise using the iterator.static booleanhasUniqueObject(Collection<?> collection) Determine whether the given Collection only contains a single unique object.static booleanisEmpty(@Nullable Collection<? extends @Nullable Object> collection) Returntrueif the supplied Collection isnullor empty.static booleanReturntrueif the supplied Map isnullor empty.static <T> @Nullable TlastElement(@Nullable List<T> list) Retrieve the last element of the given List, accessing the highest index.static <T> @Nullable TlastElement(@Nullable Set<T> set) Retrieve the last element of the given Set, usingSortedSet.last()or otherwise iterating over all elements (assuming a linked set).static <E> voidmergeArrayIntoCollection(@Nullable Object array, Collection<E> collection) Merge the given array into the given Collection.static <K,V> void mergePropertiesIntoMap(@Nullable Properties props, Map<K, V> map) Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.static <K,V> HashMap <K, V> newHashMap(int expectedSize) Instantiate a newHashMapwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.static <E> HashSet<E> newHashSet(int expectedSize) Instantiate a newHashSetwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.static <K, V extends @Nullable Object>
LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) Instantiate a newLinkedHashMapwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.static <E> LinkedHashSet<E> newLinkedHashSet(int expectedSize) Instantiate a newLinkedHashSetwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.static <A, E extends A>
A[]toArray(Enumeration<E> enumeration, A[] array) Marshal the elements from the given enumeration into an array of the given type.static <E> Iterator<E> toIterator(@Nullable Enumeration<E> enumeration) Adapt anEnumerationto anIterator.static <K,V> MultiValueMap <K, V> toMultiValueMap(Map<K, List<V>> targetMap) Adapt aMap<K, List<V>>to anMultiValueMap<K, V>.static <K,V> MultiValueMap <K, V> unmodifiableMultiValueMap(MultiValueMap<? extends K, ? extends V> targetMap) Return an unmodifiable view of the specified multi-value map.
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
isEmpty
@Contract("null -> true") public static boolean isEmpty(@Nullable Collection<? extends @Nullable Object> collection) Returntrueif the supplied Collection isnullor empty. Otherwise, returnfalse.- Parameters:
collection- the Collection to check- Returns:
- whether the given Collection is empty
-
isEmpty
-
newHashMap
Instantiate a newHashMapwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.This differs from the regular
HashMapconstructor which takes an initial capacity relative to a load factor but is effectively aligned with the JDK'sConcurrentHashMap(int).- Parameters:
expectedSize- the expected number of elements (with a corresponding capacity to be derived so that no resize/rehash operations are needed)- Since:
- 5.3
- See Also:
-
newLinkedHashMap
Instantiate a newLinkedHashMapwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.This differs from the regular
LinkedHashMapconstructor which takes an initial capacity relative to a load factor but is aligned with Spring's ownLinkedCaseInsensitiveMapandLinkedMultiValueMapconstructor semantics.- Parameters:
expectedSize- the expected number of elements (with a corresponding capacity to be derived so that no resize/rehash operations are needed)- Since:
- 5.3
- See Also:
-
newHashSet
Instantiate a newHashSetwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.- Parameters:
expectedSize- the expected number of elements (with a corresponding capacity to be derived so that no resize/rehash operations are needed)- Since:
- 6.2
- See Also:
-
newLinkedHashSet
Instantiate a newLinkedHashSetwith an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.- Parameters:
expectedSize- the expected number of elements (with a corresponding capacity to be derived so that no resize/rehash operations are needed)- Since:
- 6.2
- See Also:
-
arrayToList
Convert the supplied array into a List. A primitive array gets converted into a List of the appropriate wrapper type.NOTE: Generally prefer the standard
Arrays.asList(T...)method. ThisarrayToListmethod is just meant to deal with an incoming Object value that might be anObject[]or a primitive array at runtime.A
nullsource value will be converted to an empty List.- Parameters:
source- the (potentially primitive) array- Returns:
- the converted List result
- See Also:
-
mergeArrayIntoCollection
Merge the given array into the given Collection.- Parameters:
array- the array to merge (may benull)collection- the target Collection to merge the array into
-
mergePropertiesIntoMap
Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.Uses
Properties.propertyNames()to even catch default properties linked into the original Properties instance.- Parameters:
props- the Properties instance to merge (may benull)map- the target Map to merge the properties into
-
contains
@Contract("null, _ -> false") public static boolean contains(@Nullable Iterator<? extends @Nullable Object> iterator, @Nullable Object element) Check whether the givenIteratorcontains the given element.- Parameters:
iterator- theIteratorto checkelement- the element to look for- Returns:
trueif found,falseotherwise
-
contains
@Contract("null, _ -> false") public static boolean contains(@Nullable Enumeration<? extends @Nullable Object> enumeration, @Nullable Object element) Check whether the givenEnumerationcontains the given element.- Parameters:
enumeration- theEnumerationto checkelement- the element to look for- Returns:
trueif found,falseotherwise
-
containsInstance
@Contract("null, _ -> false") public static boolean containsInstance(@Nullable Collection<? extends @Nullable Object> collection, @Nullable Object element) Check whether the givenCollectioncontains the given element instance.Enforces the given instance to be present, rather than returning
truefor an equal element as well.- Parameters:
collection- theCollectionto checkelement- the element to look for- Returns:
trueif found,falseotherwise
-
containsAny
public static boolean containsAny(Collection<? extends @Nullable Object> source, Collection<? extends @Nullable Object> candidates) Returntrueif any element in 'candidates' is contained in 'source'; otherwise returnsfalse.- Parameters:
source- the sourceCollectioncandidates- the candidates to search for- Returns:
- whether any of the candidates has been found
-
findFirstMatch
Return the first element in 'candidates' that is contained in 'source'. If no element in 'candidates' is present in 'source' returnsnull. Iteration order isCollectionimplementation specific.- Parameters:
source- the source Collectioncandidates- the candidates to search for- Returns:
- the first present object, or
nullif not found
-
findValueOfType
@Contract("null, _ -> null") public static <T> @Nullable T findValueOfType(@Nullable Collection<?> collection, @Nullable Class<T> type) Find a single value of the given type in the given Collection.- Parameters:
collection- the Collection to searchtype- the type to look for- Returns:
- a value of the given type found if there is a clear match,
or
nullif none or more than one such value found
-
findValueOfType
Find a single value of one of the given types in the given Collection: searching the Collection for a value of the first type, then searching for a value of the second type, etc.- Parameters:
collection- the collection to searchtypes- the types to look for, in prioritized order- Returns:
- a value of one of the given types found if there is a clear match,
or
nullif none or more than one such value found
-
hasUniqueObject
Determine whether the given Collection only contains a single unique object.- Parameters:
collection- the Collection to check- Returns:
trueif the collection contains a single reference or multiple references to the same instance,falseotherwise
-
findCommonElementType
Find the common element type of the given Collection, if any.- Parameters:
collection- the Collection to check- Returns:
- the common element type, or
nullif no clear common type has been found (or the collection was empty)
-
firstElement
Retrieve the first element of the given Set, usingSortedSet.first()or otherwise using the iterator.- Parameters:
set- the Set to check (may benullor empty)- Returns:
- the first element, or
nullif none - Since:
- 5.2.3
- See Also:
-
firstElement
-
lastElement
Retrieve the last element of the given Set, usingSortedSet.last()or otherwise iterating over all elements (assuming a linked set).- Parameters:
set- the Set to check (may benullor empty)- Returns:
- the last element, or
nullif none - Since:
- 5.0.3
- See Also:
-
lastElement
-
toArray
Marshal the elements from the given enumeration into an array of the given type. Enumeration elements must be assignable to the type of the given array. The array returned will be a different instance than the array given. -
toIterator
Adapt anEnumerationto anIterator.- Parameters:
enumeration- the originalEnumeration- Returns:
- the adapted
Iterator
-
toMultiValueMap
Adapt aMap<K, List<V>>to anMultiValueMap<K, V>.- Parameters:
targetMap- the original map- Returns:
- the adapted multi-value map (wrapping the original map)
- Since:
- 3.1
-
unmodifiableMultiValueMap
public static <K,V> MultiValueMap<K,V> unmodifiableMultiValueMap(MultiValueMap<? extends K, ? extends V> targetMap) Return an unmodifiable view of the specified multi-value map.- Parameters:
targetMap- the map for which an unmodifiable view is to be returned.- Returns:
- an unmodifiable view of the specified multi-value map
- Since:
- 3.1
-
compositeMap
Return a (partially unmodifiable) map that combines the provided two maps. InvokingMap.put(Object, Object)orMap.putAll(Map)on the returned map results in anUnsupportedOperationException.In the case of a key collision,
firsttakes precedence oversecond. In other words, entries insecondwith a key that is also mapped byfirstare effectively ignored.- Parameters:
first- the first map to composesecond- the second map to compose- Returns:
- a new map that composes the given two maps
- Since:
- 6.2
-
compositeMap
public static <K,V> Map<K,V> compositeMap(Map<K, V> first, Map<K, V> second, @Nullable BiFunction<K, V, V> putFunction, @Nullable Consumer<Map<K, V>> putAllFunction) Return a map that combines the provided maps. InvokingMap.put(Object, Object)on the returned map will applyputFunction, or will throw anUnsupportedOperationExceptionputFunctionisnull. The same applies toMap.putAll(Map)andputAllFunction.In the case of a key collision,
firsttakes precedence oversecond. In other words, entries insecondwith a key that is also mapped byfirstare effectively ignored.- Parameters:
first- the first map to composesecond- the second map to composeputFunction- applied whenMap::putis invoked. Ifnull,Map::putthrows anUnsupportedOperationException.putAllFunction- applied whenMap::putAllis invoked. Ifnull,Map::putAllthrows anUnsupportedOperationException.- Returns:
- a new map that composes the give maps
- Since:
- 6.2
-