类 CollectionUtils
- java.lang.Object
- org.springframework.util.CollectionUtils
public abstract class CollectionUtils extends Object
Miscellaneous collection utility methods. Mainly for internal use within the framework.- 从以下版本开始:
- 1.1.3
- 作者:
- Juergen Hoeller, Rob Harrop, Arjen Poutsma
构造器概要
构造器 构造器 说明 CollectionUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static List
arrayToList(Object source)
Convert the supplied array into a List.static boolean
contains(Enumeration<?> enumeration, Object element)
Check whether the given Enumeration contains the given element.static boolean
contains(Iterator<?> iterator, Object element)
Check whether the given Iterator contains the given element.static boolean
containsAny(Collection<?> source, Collection<?> candidates)
Returntrue
if any element in 'candidates
' is contained in 'source
'; otherwise returnsfalse
.static boolean
containsInstance(Collection<?> collection, Object element)
Check whether the given Collection contains the given element instance.static Class<?>
findCommonElementType(Collection<?> collection)
Find the common element type of the given Collection, if any.static <E> E
findFirstMatch(Collection<?> source, Collection<E> candidates)
Return the first element in 'candidates
' that is contained in 'source
'.static Object
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> T
findValueOfType(Collection<?> collection, Class<T> type)
Find a single value of the given type in the given Collection.static <T> T
firstElement(List<T> list)
Retrieve the first element of the given List, accessing the zero index.static <T> T
firstElement(Set<T> set)
Retrieve the first element of the given Set, usingSortedSet.first()
or otherwise using the iterator.static boolean
hasUniqueObject(Collection<?> collection)
Determine whether the given Collection only contains a single unique object.static boolean
isEmpty(Collection<?> collection)
Returntrue
if the supplied Collection isnull
or empty.static boolean
isEmpty(Map<?,?> map)
Returntrue
if the supplied Map isnull
or empty.static <T> T
lastElement(List<T> list)
Retrieve the last element of the given List, accessing the highest index.static <T> T
lastElement(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> void
mergeArrayIntoCollection(Object array, Collection<E> collection)
Merge the given array into the given Collection.static <K,V>
voidmergePropertiesIntoMap(Properties props, Map<K,V> map)
Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.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(Enumeration<E> enumeration)
Adapt anEnumeration
to 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.
构造器详细资料
CollectionUtils
public CollectionUtils()
方法详细资料
isEmpty
public static boolean isEmpty(@Nullable Collection<?> collection)
Returntrue
if the supplied Collection isnull
or empty. Otherwise, returnfalse
.- 参数:
collection
- the Collection to check- 返回:
- whether the given Collection is empty
isEmpty
public static boolean isEmpty(@Nullable Map<?,?> map)
Returntrue
if the supplied Map isnull
or empty. Otherwise, returnfalse
.- 参数:
map
- the Map to check- 返回:
- whether the given Map is empty
arrayToList
public static List arrayToList(@Nullable Object source)
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. ThisarrayToList
method is just meant to deal with an incoming Object value that might be anObject[]
or a primitive array at runtime.A
null
source value will be converted to an empty List.- 参数:
source
- the (potentially primitive) array- 返回:
- the converted List result
- 另请参阅:
ObjectUtils.toObjectArray(Object)
,Arrays.asList(Object[])
mergeArrayIntoCollection
public static <E> void mergeArrayIntoCollection(@Nullable Object array, Collection<E> collection)
Merge the given array into the given Collection.- 参数:
array
- the array to merge (may benull
)collection
- the target Collection to merge the array into
mergePropertiesIntoMap
public 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.Uses
Properties.propertyNames()
to even catch default properties linked into the original Properties instance.- 参数:
props
- the Properties instance to merge (may benull
)map
- the target Map to merge the properties into
contains
public static boolean contains(@Nullable Iterator<?> iterator, Object element)
Check whether the given Iterator contains the given element.- 参数:
iterator
- the Iterator to checkelement
- the element to look for- 返回:
true
if found,false
otherwise
contains
public static boolean contains(@Nullable Enumeration<?> enumeration, Object element)
Check whether the given Enumeration contains the given element.- 参数:
enumeration
- the Enumeration to checkelement
- the element to look for- 返回:
true
if found,false
otherwise
containsInstance
public static boolean containsInstance(@Nullable Collection<?> collection, Object element)
Check whether the given Collection contains the given element instance.Enforces the given instance to be present, rather than returning
true
for an equal element as well.- 参数:
collection
- the Collection to checkelement
- the element to look for- 返回:
true
if found,false
otherwise
containsAny
public static boolean containsAny(Collection<?> source, Collection<?> candidates)
Returntrue
if any element in 'candidates
' is contained in 'source
'; otherwise returnsfalse
.- 参数:
source
- the source Collectioncandidates
- the candidates to search for- 返回:
- whether any of the candidates has been found
findFirstMatch
@Nullable public static <E> E findFirstMatch(Collection<?> source, Collection<E> candidates)
Return the first element in 'candidates
' that is contained in 'source
'. If no element in 'candidates
' is present in 'source
' returnsnull
. Iteration order isCollection
implementation specific.- 参数:
source
- the source Collectioncandidates
- the candidates to search for- 返回:
- the first present object, or
null
if not found
findValueOfType
@Nullable public static <T> T findValueOfType(Collection<?> collection, @Nullable Class<T> type)
Find a single value of the given type in the given Collection.- 参数:
collection
- the Collection to searchtype
- the type to look for- 返回:
- a value of the given type found if there is a clear match, or
null
if none or more than one such value found
findValueOfType
@Nullable public static Object 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.- 参数:
collection
- the collection to searchtypes
- the types to look for, in prioritized order- 返回:
- a value of one of the given types found if there is a clear match, or
null
if none or more than one such value found
hasUniqueObject
public static boolean hasUniqueObject(Collection<?> collection)
Determine whether the given Collection only contains a single unique object.- 参数:
collection
- the Collection to check- 返回:
true
if the collection contains a single reference or multiple references to the same instance,false
otherwise
findCommonElementType
@Nullable public static Class<?> findCommonElementType(Collection<?> collection)
Find the common element type of the given Collection, if any.- 参数:
collection
- the Collection to check- 返回:
- the common element type, or
null
if no clear common type has been found (or the collection was empty)
firstElement
@Nullable public static <T> T firstElement(@Nullable Set<T> set)
Retrieve the first element of the given Set, usingSortedSet.first()
or otherwise using the iterator.- 参数:
set
- the Set to check (may benull
or empty)- 返回:
- the first element, or
null
if none - 从以下版本开始:
- 5.2.3
- 另请参阅:
SortedSet
,LinkedHashMap.keySet()
,LinkedHashSet
firstElement
@Nullable public static <T> T firstElement(@Nullable List<T> list)
Retrieve the first element of the given List, accessing the zero index.- 参数:
list
- the List to check (may benull
or empty)- 返回:
- the first element, or
null
if none - 从以下版本开始:
- 5.2.3
lastElement
@Nullable public static <T> T lastElement(@Nullable Set<T> set)
Retrieve the last element of the given Set, usingSortedSet.last()
or otherwise iterating over all elements (assuming a linked set).- 参数:
set
- the Set to check (may benull
or empty)- 返回:
- the last element, or
null
if none - 从以下版本开始:
- 5.0.3
- 另请参阅:
SortedSet
,LinkedHashMap.keySet()
,LinkedHashSet
lastElement
@Nullable public static <T> T lastElement(@Nullable List<T> list)
Retrieve the last element of the given List, accessing the highest index.- 参数:
list
- the List to check (may benull
or empty)- 返回:
- the last element, or
null
if none - 从以下版本开始:
- 5.0.3
toArray
public 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. 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
public static <E> Iterator<E> toIterator(@Nullable Enumeration<E> enumeration)
Adapt anEnumeration
to anIterator
.- 参数:
enumeration
- the originalEnumeration
- 返回:
- the adapted
Iterator
toMultiValueMap
public static <K,V> MultiValueMap<K,V> toMultiValueMap(Map<K,List<V>> targetMap)
Adapt aMap<K, List<V>>
to anMultiValueMap<K, V>
.- 参数:
targetMap
- the original map- 返回:
- the adapted multi-value map (wrapping the original map)
- 从以下版本开始:
- 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.- 参数:
targetMap
- the map for which an unmodifiable view is to be returned.- 返回:
- an unmodifiable view of the specified multi-value map
- 从以下版本开始:
- 3.1