public final class CollectionUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static Collection |
EMPTY_COLLECTION
已过时。
use
Collections.EMPTY_LIST or Collections.emptyList() instead |
static List |
EMPTY_LIST
已过时。
use
Collections.EMPTY_LIST or Collections.emptyList() instead |
static Map |
EMPTY_MAP
已过时。
use
Collections.EMPTY_MAP or Collections.emptyMap() instead |
static float |
LOAD_FACTOR |
static int |
MINIMUM_INITIAL_CAPACITY |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> ArrayList<T> |
arrayList(int anticipatedSize) |
static List |
arrayToList(Object source)
Convert the supplied array into a List.
|
static <K,V> ConcurrentHashMap<K,V> |
concurrentMap(int expectedNumberOfElements)
Create a properly sized
ConcurrentHashMap based on the given expected number of elements. |
static <K,V> ConcurrentHashMap<K,V> |
concurrentMap(int expectedNumberOfElements,
float loadFactor)
Create a properly sized
ConcurrentHashMap based on the given expected number of elements and an
explicit load factor |
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)
Return
true if any element in 'candidates' is
contained in 'source'; otherwise returns false. |
static boolean |
containsInstance(Collection<?> collection,
Object element)
Check whether the given Collection contains the given element instance.
|
static int |
determineProperSizing(int numberOfElements)
Determine the proper initial size for a new collection in order for it to hold the given a number of elements.
|
static int |
determineProperSizing(Map original)
Given a map, determine the proper initial size for a new Map to hold the same number of values.
|
static int |
determineProperSizing(Set original)
Given a set, determine the proper initial size for a new set to hold the same number of values.
|
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 boolean |
hasUniqueObject(Collection<?> collection)
Determine whether the given Collection only contains a single unique object.
|
static boolean |
isEmpty(Collection<?> collection)
Return
true if the supplied Collection is null or empty. |
static boolean |
isEmpty(Map<?,?> map)
Return
true if the supplied Map is null or empty. |
static boolean |
isEmpty(Object[] objects) |
static boolean |
isNotEmpty(Collection collection) |
static boolean |
isNotEmpty(Map map) |
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, using
SortedSet.last()
or otherwise iterating over all elements (assuming a linked set). |
static <X,Y> Map<X,Y> |
makeCopy(Map<X,Y> map) |
static <T> Set<T> |
makeCopy(Set<T> source) |
static <K,V> Map<K,V> |
mapOfSize(int size)
Build a properly sized map, especially handling load size and load factor to prevent immediate resizing.
|
static <E> void |
mergeArrayIntoCollection(Object array,
Collection<E> collection)
Merge the given array into the given Collection.
|
static <K,V> void |
mergePropertiesIntoMap(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> |
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 an enumeration to an iterator.
|
public static final int MINIMUM_INITIAL_CAPACITY
public static final float LOAD_FACTOR
@Deprecated public static final List EMPTY_LIST
Collections.EMPTY_LIST or Collections.emptyList() instead@Deprecated public static final Collection EMPTY_COLLECTION
Collections.EMPTY_LIST or Collections.emptyList() instead@Deprecated public static final Map EMPTY_MAP
Collections.EMPTY_MAP or Collections.emptyMap() insteadpublic static <K,V> Map<K,V> mapOfSize(int size)
size - The size to make the map.public static int determineProperSizing(Map original)
original - The original mappublic static int determineProperSizing(Set original)
original - The original setpublic static int determineProperSizing(int numberOfElements)
numberOfElements - The number of elements to be stored.public static <K,V> ConcurrentHashMap<K,V> concurrentMap(int expectedNumberOfElements)
ConcurrentHashMap based on the given expected number of elements.K - The map key typeV - The map value typeexpectedNumberOfElements - The expected number of elements for the created mappublic static <K,V> ConcurrentHashMap<K,V> concurrentMap(int expectedNumberOfElements, float loadFactor)
ConcurrentHashMap based on the given expected number of elements and an
explicit load factorK - The map key typeV - The map value typeexpectedNumberOfElements - The expected number of elements for the created maploadFactor - The collection load factorpublic static <T> ArrayList<T> arrayList(int anticipatedSize)
public static boolean isNotEmpty(Collection collection)
public static boolean isNotEmpty(Map map)
public static boolean isEmpty(Object[] objects)
public static boolean isEmpty(Collection<?> collection)
true if the supplied Collection is null or empty.
Otherwise, return false.collection - the Collection to checkpublic static boolean isEmpty(Map<?,?> map)
true if the supplied Map is null or empty.
Otherwise, return false.map - the Map to checkpublic static List arrayToList(Object source)
NOTE: Generally prefer the standard Arrays.asList(T...) method.
This arrayToList method is just meant to deal with an incoming Object
value that might be an Object[] or a primitive array at runtime.
A null source value will be converted to an empty List.
source - the (potentially primitive) arrayObjectUtils.toObjectArray(Object),
Arrays.asList(Object[])public static <E> void mergeArrayIntoCollection(Object array, Collection<E> collection)
array - the array to merge (may be null)collection - the target Collection to merge the array intopublic static <K,V> void mergePropertiesIntoMap(Properties props, Map<K,V> map)
Uses Properties.propertyNames() to even catch
default properties linked into the original Properties instance.
props - the Properties instance to merge (may be null)map - the target Map to merge the properties intopublic static boolean contains(Iterator<?> iterator, Object element)
iterator - the Iterator to checkelement - the element to look fortrue if found, false otherwisepublic static boolean contains(Enumeration<?> enumeration, Object element)
enumeration - the Enumeration to checkelement - the element to look fortrue if found, false otherwisepublic static boolean containsInstance(Collection<?> collection, Object element)
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 fortrue if found, false otherwisepublic static boolean containsAny(Collection<?> source, Collection<?> candidates)
true if any element in 'candidates' is
contained in 'source'; otherwise returns false.source - the source Collectioncandidates - the candidates to search forpublic static <E> E findFirstMatch(Collection<?> source, Collection<E> candidates)
candidates' that is contained in
'source'. If no element in 'candidates' is present in
'source' returns null. Iteration order is
Collection implementation specific.source - the source Collectioncandidates - the candidates to search fornull if not foundpublic static <T> T findValueOfType(Collection<?> collection, Class<T> type)
collection - the Collection to searchtype - the type to look fornull if none or more than one such value foundpublic static Object findValueOfType(Collection<?> collection, Class<?>[] types)
collection - the collection to searchtypes - the types to look for, in prioritized ordernull if none or more than one such value foundpublic static boolean hasUniqueObject(Collection<?> collection)
collection - the Collection to checktrue if the collection contains a single reference or
multiple references to the same instance, false otherwisepublic static Class<?> findCommonElementType(Collection<?> collection)
collection - the Collection to checknull if no clear
common type has been found (or the collection was empty)public static <T> T lastElement(Set<T> set)
SortedSet.last()
or otherwise iterating over all elements (assuming a linked set).set - the Set to check (may be null or empty)null if noneSortedSet,
LinkedHashMap.keySet(),
LinkedHashSetpublic static <T> T lastElement(List<T> list)
list - the List to check (may be null or empty)null if nonepublic static <A,E extends A> A[] toArray(Enumeration<E> enumeration, A[] array)
public static <E> Iterator<E> toIterator(Enumeration<E> enumeration)
enumeration - the enumerationCopyright © 2020. All rights reserved.