public abstract class CollectionUtils
extends org.springframework.util.CollectionUtils
Collection
,
Collections
,
Enumeration
,
Iterator
,
List
,
Map
,
Set
,
CollectionUtils
Constructor and Description |
---|
CollectionUtils() |
Modifier and Type | Method and Description |
---|---|
static <E,T extends java.util.Collection<E>> |
addAll(T collection,
java.lang.Iterable<E> iterable)
Adds all elements from the given
Iterable to the Collection . |
static <T> java.util.Set<T> |
asSet(T... elements)
Returns an unmodifiable
Set containing the elements from the given object array. |
static boolean |
containsAny(java.util.Collection<?> collection,
java.lang.Object... elements)
Null-safe method to determines whether the given
Collection contains any elements from the given array. |
static <T> java.lang.Iterable<T> |
emptyIterable()
Returns an empty
Iterable object. |
static <T> java.lang.Iterable<T> |
iterable(java.util.Enumeration<T> enumeration)
Adapts the given Enumeration as an Iterable object for use within a for each loop.
|
static <T> java.lang.Iterable<T> |
iterable(java.util.Iterator<T> iterator)
Adapts the given Iterator as an Iterable object for use within a for each loop.
|
static <T> java.util.Collection<T> |
nullSafeCollection(java.util.Collection<T> collection)
Null-safe operation returning the given
Collection if not null
or an empty Collection (implemented with List ) if null. |
static <T> java.util.Enumeration<T> |
nullSafeEnumeration(java.util.Enumeration<T> enumeration)
Null-safe operation returning the given
Enumeration if not null
or an empty Enumeration if null. |
static boolean |
nullSafeIsEmpty(java.util.Collection<?> collection)
Determines whether the given
Collection is empty . |
static boolean |
nullSafeIsEmpty(java.lang.Iterable<?> iterable)
Determines whether the given
Iterable is empty. |
static boolean |
nullSafeIsEmpty(java.util.Map<?,?> map)
Determines whether the given
Map is empty . |
static <T> java.lang.Iterable<T> |
nullSafeIterable(java.lang.Iterable<T> iterable)
A null-safe operation returning the original Iterable object if non-null or a default, empty Iterable
implementation if null.
|
static <E,T extends java.lang.Iterable<E>> |
nullSafeIterable(T iterable,
T defaultIterable)
Returns the given
Iterable if not null or empty,
otherwise returns the defaultIterable . |
static <T> java.util.Iterator<T> |
nullSafeIterator(java.util.Iterator<T> iterator)
Null-safe operation returning the given
Iterator if not null
or an empty Iterator if null. |
static <T> java.util.List<T> |
nullSafeList(java.util.List<T> list)
Null-safe operation returning the given
List if not null
or an empty List if null. |
static <K,V> java.util.Map<K,V> |
nullSafeMap(java.util.Map<K,V> map)
Null-safe operation returning the given
Map if not null
or an empty Map if null. |
static <T> java.util.Set<T> |
nullSafeSet(java.util.Set<T> set)
Null-safe operation returning the given
Set if not null
or an empty Set if null. |
static int |
nullSafeSize(java.util.Collection<?> collection)
Determines the
Collection.size() of the given Collection . |
static long |
nullSafeSize(java.lang.Iterable iterable)
Determines the
size of the give Iterable . |
static int |
nullSafeSize(java.util.Map<?,?> map)
Determines the
Map.size() of the given Map . |
static <T extends java.lang.Comparable<T>> |
sort(java.util.List<T> list)
Sors the elements of the given
List by their natural, Comparable ordering. |
static <T> java.util.List<T> |
subList(java.util.List<T> source,
int... indices)
Returns a sub-list of elements from the given
List based on the provided indices . |
static java.lang.String |
toString(java.util.Map<?,?> map)
Returns a
String representation of the given Map . |
arrayToList, contains, contains, containsAny, containsInstance, findCommonElementType, findFirstMatch, findValueOfType, findValueOfType, firstElement, firstElement, hasUniqueObject, isEmpty, isEmpty, lastElement, lastElement, mergeArrayIntoCollection, mergePropertiesIntoMap, newHashMap, newLinkedHashMap, toArray, toIterator, toMultiValueMap, unmodifiableMultiValueMap
@NonNull public static <E,T extends java.util.Collection<E>> T addAll(@NonNull T collection, @Nullable java.lang.Iterable<E> iterable)
Iterable
to the Collection
.E
- Class
type of the elements in the Collection
and Iterable
.T
- concrete Class
type of the Collection
.collection
- Collection
in which to add the elements from the Iterable
.iterable
- Iterable
containing the elements to add to the Collection
.Collection
.java.lang.IllegalArgumentException
- if Collection
is null.Iterable
,
Collection
@SafeVarargs @NonNull public static <T> java.util.Set<T> asSet(@NonNull T... elements)
Set
containing the elements from the given object array.T
- Class type of the elements.elements
- array of objects to add to the Set
.Set
containing the elements from the given object array.public static boolean containsAny(@Nullable java.util.Collection<?> collection, @Nullable java.lang.Object... elements)
Collection
contains any elements from the given array.collection
- Collection
to evaluateelements
- array of elements to evaluate.Collection.contains(Object)
@NonNull public static <T> java.lang.Iterable<T> emptyIterable()
Iterable
object.T
- Class
type of the elements in the Iterable
.Iterable
.Iterable
,
nullSafeIterable(Iterable)
@NonNull public static <T> java.lang.Iterable<T> iterable(@Nullable java.util.Enumeration<T> enumeration)
T
- the class type of the Enumeration elements.enumeration
- the Enumeration to adapt as an Iterable object.Iterable
,
Enumeration
@NonNull public static <T> java.lang.Iterable<T> iterable(@Nullable java.util.Iterator<T> iterator)
T
- the class type of the Iterator elements.iterator
- the Iterator to adapt as an Iterable object.Iterable
,
Iterator
@NonNull public static <T> java.util.Collection<T> nullSafeCollection(@Nullable java.util.Collection<T> collection)
Collection
if not null
or an empty Collection
(implemented with List
) if null.T
- Class type of the Collection
elements.collection
- Collection
to evaluate.Collection
if not null or return an empty Collection
(implemented with List
).Collections.emptyList()
,
Collection
@NonNull public static <T> java.util.Enumeration<T> nullSafeEnumeration(@Nullable java.util.Enumeration<T> enumeration)
Enumeration
if not null
or an empty Enumeration
if null.T
- type
of elements contained in the Enumeration
.enumeration
- Enumeration
to evaluate.Enumeration
if not null
or an empty Enumeration
.Collections.emptyEnumeration()
,
Enumeration
@NonNull public static <T> java.lang.Iterable<T> nullSafeIterable(@Nullable java.lang.Iterable<T> iterable)
T
- the class type of the iterable elements.iterable
- the Iterable object evaluated for a null reference.emptyIterable()
,
Iterable
@Nullable public static <E,T extends java.lang.Iterable<E>> T nullSafeIterable(@Nullable T iterable, @Nullable T defaultIterable)
Iterable
if not null or empty,
otherwise returns the defaultIterable
.T
- concrete Class
type of the Iterable
.E
- Class
type of the elements in the Iterables
.iterable
- Iterable
to evaluate.defaultIterable
- Iterable
to return if the given iterable
is null or empty.iterable
if not null or empty otherwise return defaultIterable
.Iterable
@NonNull public static <T> java.util.Iterator<T> nullSafeIterator(@Nullable java.util.Iterator<T> iterator)
Iterator
if not null
or an empty Iterator
if null.T
- type
of elements contained in the Iterator
.iterator
- Iterator
to evaluate.Iterator
if not null
or an empty Iterator
.Collections.emptyIterator()
,
Iterator
@NonNull public static <T> java.util.List<T> nullSafeList(@Nullable java.util.List<T> list)
List
if not null
or an empty List
if null.T
- Class type of the List
elements.list
- List
to evaluate.List
if not null or an empty List
.Collections.emptyList()
,
List
@NonNull public static <K,V> java.util.Map<K,V> nullSafeMap(@Nullable java.util.Map<K,V> map)
Map
if not null
or an empty Map
if null.K
- Class type of the Map's
keys.V
- Class type of the Map's
values.map
- Map
to evaluate.Map
if not null or an empty Map
.Collections.emptyMap()
,
Map
@NonNull public static <T> java.util.Set<T> nullSafeSet(@Nullable java.util.Set<T> set)
Set
if not null
or an empty Set
if null.T
- Class type of the Set
elements.set
- Set
to evaluate.Set
if not null or an empty Set
.Collections.emptySet()
,
Set
public static boolean nullSafeIsEmpty(@Nullable java.util.Collection<?> collection)
Collection
is empty
.collection
- Collection
to evaluate.Collection
is empty
.nullSafeCollection(Collection)
,
Collection.isEmpty()
public static boolean nullSafeIsEmpty(@Nullable java.lang.Iterable<?> iterable)
Iterable
is empty.iterable
- Iterable
to evaluate.Iterable
is empty.Iterable
,
nullSafeIterable(Iterable)
public static boolean nullSafeIsEmpty(@Nullable java.util.Map<?,?> map)
Map
is empty
.map
- Map
to evaluate.Map
is empty
.nullSafeMap(Map)
,
Map.isEmpty()
public static int nullSafeSize(@Nullable java.util.Collection<?> collection)
Collection.size()
of the given Collection
.collection
- Collection
to evaluate.Collection.size()
of the given Collection
.nullSafeCollection(Collection)
,
Collection.size()
public static long nullSafeSize(@Nullable java.lang.Iterable iterable)
size
of the give Iterable
.iterable
- Iterable
to evaluate.size
indicating the number of elements contained by the given Iterable
.
If Iterable
is null, then returns 0.Iterable
public static int nullSafeSize(@Nullable java.util.Map<?,?> map)
Map.size()
of the given Map
.map
- Map
to evaluate.Map.size()
of the given Map
.nullSafeMap(Map)
,
Map.size()
@NonNull public static <T extends java.lang.Comparable<T>> java.util.List<T> sort(@NonNull java.util.List<T> list)
List
by their natural, Comparable
ordering.T
- Comparable
class type of the collection elements.list
- List
of elements to sort.List
sorted.Collections.sort(List)
,
List
@NonNull public static <T> java.util.List<T> subList(@NonNull java.util.List<T> source, int... indices)
List
based on the provided indices
.T
- Class type of the elements in the list.source
- List
from which the elements of the sub-list is constructed.indices
- array of indexes in the source
List
to the elements
used to construct the sub-list.List
based on the provided indices
.java.lang.IndexOutOfBoundsException
- if the array of indexes contains an index that is not within
the bounds of the list.java.lang.NullPointerException
- if either the list or indexes are null.List
@NonNull public static java.lang.String toString(@Nullable java.util.Map<?,?> map)
String
representation of the given Map
.map
- Map
represent as a String
.String
describing the given Map
.newSortedMap(Map)
,
Map