public class CollectionUtils extends Object
限定符和类型 | 方法和说明 |
---|---|
static List |
arrayToList(Object source)
Convert the supplied array into a List.
|
static <E> List<List<E>> |
batchSplit(List<E> list,
int batchSize)
将一个集合按照指定量分为多个批次
|
static <E> List<List<E>> |
batchSplitByGuava(List<E> list,
int batchSize)
将一个集合按照指定量分为多个批次
|
static <E> List<List<E>> |
batchSplitByStream(List<E> list,
int batchSize)
将一个集合按照指定量分为多个批次
|
static <E> Set<E> |
complement(Set<E> set1,
Set<E> set2)
求两个集合的非公共元素构成的集合
|
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 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, using
SortedSet.first()
or otherwise using the iterator. |
static boolean |
hasUniqueObject(Collection<?> collection)
Determine whether the given Collection only contains a single unique object.
|
static <E> Set<E> |
intersect(Set<E> set1,
Set<E> set2)
获取两个集合的交集
|
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 |
isNotEmpty(Collection<?> collection)
检测是否不为空
|
static boolean |
isNotEmpty(Map<?,?> map)
检测是否不为空
|
static boolean |
isNullOrEmpty(Collection<?> c)
判断集合是否为空
|
static boolean |
isNullOrEmpty(Map<?,?> map)
判断集合是否为空
|
static boolean |
isNullOrEmpty(Object[] objects)
判断集合是否为空
|
static <E> boolean |
isSubSet(Set<E> father,
Set<E> child)
child是否是father的子集
|
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 <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 <E> Set<E> |
minus(Set<E> set1,
Set<E> set2)
求两个集合的差集,set1相对set2的差集
|
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 . |
static <E> Set<E> |
union(Set<E> set1,
Set<E> set2)
获取两个集合的并集
|
static <E> Set<E> |
unique(E[] array)
唯一化一个数组
|
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 isNotEmpty(Collection<?> collection)
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 boolean isNotEmpty(Map<?,?> map)
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 firstElement(Set<T> set)
SortedSet.first()
or otherwise using the iterator.set
- the Set to check (may be null
or empty)null
if noneSortedSet
,
LinkedHashMap.keySet()
,
LinkedHashSet
public static <T> T firstElement(List<T> list)
list
- the List to check (may be null
or empty)null
if nonepublic 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()
,
LinkedHashSet
public 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
to an Iterator
.enumeration
- the original Enumeration
Iterator
public static boolean isNullOrEmpty(Collection<?> c)
c
- 待判断集合public static boolean isNullOrEmpty(Map<?,?> map)
map
- 待判断集合public static boolean isNullOrEmpty(Object[] objects)
objects
- 待判断集合public static <E> Set<E> unique(E[] array)
E
- array
- 待处理数组public static <E> boolean isSubSet(Set<E> father, Set<E> child)
E
- father
- 父集child
- 子集public static <E> Set<E> intersect(Set<E> set1, Set<E> set2)
E
- set1
- 待处理集合1set2
- 待处理集合2public static <E> Set<E> union(Set<E> set1, Set<E> set2)
E
- set1
- 待处理集合1set2
- 待处理集合2public static <E> Set<E> minus(Set<E> set1, Set<E> set2)
E
- set1
- 待处理集合1set2
- 待处理集合2public static <E> Set<E> complement(Set<E> set1, Set<E> set2)
E
- set1
- 待处理集合1set2
- 待处理集合2public static <E> List<List<E>> batchSplit(List<E> list, int batchSize)
E
- list
- 集合batchSize
- 批量大小public static <E> List<List<E>> batchSplitByStream(List<E> list, int batchSize)
E
- list
- 集合batchSize
- 批量大小