Class CollectionUtils
java.lang.Object
org.springframework.util.CollectionUtils
org.springframework.data.gemfire.util.CollectionUtils
public abstract class CollectionUtils
extends org.springframework.util.CollectionUtils
Abstract utility class used to operate on Java Collections Framework and classes.
- Since:
- 1.7.0
- See Also:
-
Collection
Collections
Enumeration
Iterator
List
Map
Set
CollectionUtils
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E,
T extends Collection<E>>
TAdds all elements from the givenIterable
to theCollection
.static <T> Set<T>
asSet
(T... elements) Returns an unmodifiableSet
containing the elements from the given object array.static boolean
containsAny
(Collection<?> collection, Object... elements) Null-safe method to determines whether the givenCollection
contains any elements from the given array.static <T> Iterable<T>
Returns an emptyIterable
object.static <T> Iterable<T>
iterable
(Enumeration<T> enumeration) Adapts the given Enumeration as an Iterable object for use within a for each loop.static <T> Iterable<T>
Adapts the given Iterator as an Iterable object for use within a for each loop.static <T> Collection<T>
nullSafeCollection
(Collection<T> collection) Null-safe operation returning the givenCollection
if not null or an emptyCollection
(implemented withList
) if null.static <T> Enumeration<T>
nullSafeEnumeration
(Enumeration<T> enumeration) Null-safe operation returning the givenEnumeration
if not null or anempty Enumeration
if null.static boolean
nullSafeIsEmpty
(Iterable<?> iterable) Determines whether the givenIterable
is empty.static boolean
nullSafeIsEmpty
(Collection<?> collection) Determines whether the givenCollection
isempty
.static boolean
nullSafeIsEmpty
(Map<?, ?> map) static <T> Iterable<T>
nullSafeIterable
(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 Iterable<E>>
TnullSafeIterable
(T iterable, T defaultIterable) Returns the givenIterable
if not null or empty, otherwise returns thedefaultIterable
.static <T> Iterator<T>
nullSafeIterator
(Iterator<T> iterator) Null-safe operation returning the givenIterator
if not null or anempty Iterator
if null.static <T> List<T>
nullSafeList
(List<T> list) static <K,
V> Map<K, V> nullSafeMap
(Map<K, V> map) static <T> Set<T>
nullSafeSet
(Set<T> set) static long
nullSafeSize
(Iterable iterable) static int
nullSafeSize
(Collection<?> collection) Determines theCollection.size()
of the givenCollection
.static int
nullSafeSize
(Map<?, ?> map) Determines theMap.size()
of the givenMap
.static <T extends Comparable<T>>
List<T>Sors the elements of the givenList
by their natural,Comparable
ordering.static <T> List<T>
Returns a sub-list of elements from the givenList
based on the providedindices
.static String
Methods inherited from class org.springframework.util.CollectionUtils
arrayToList, contains, contains, containsAny, containsInstance, findCommonElementType, findFirstMatch, findValueOfType, findValueOfType, firstElement, firstElement, hasUniqueObject, isEmpty, isEmpty, lastElement, lastElement, mergeArrayIntoCollection, mergePropertiesIntoMap, newHashMap, newLinkedHashMap, toArray, toIterator, toMultiValueMap, unmodifiableMultiValueMap
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
addAll
@NonNull public static <E,T extends Collection<E>> T addAll(@NonNull T collection, @Nullable Iterable<E> iterable) Adds all elements from the givenIterable
to theCollection
.- Type Parameters:
E
-Class
type of the elements in theCollection
andIterable
.T
- concreteClass
type of theCollection
.- Parameters:
collection
-Collection
in which to add the elements from theIterable
.iterable
-Iterable
containing the elements to add to theCollection
.- Returns:
- the given
Collection
. - Throws:
IllegalArgumentException
- ifCollection
is null.- See Also:
-
asSet
Returns an unmodifiableSet
containing the elements from the given object array. -
containsAny
Null-safe method to determines whether the givenCollection
contains any elements from the given array.- Parameters:
collection
-Collection
to evaluateelements
- array of elements to evaluate.- Returns:
- a boolean value indicating whether the collection contains at least 1 element from the given array.
- See Also:
-
emptyIterable
Returns an emptyIterable
object. -
iterable
Adapts the given Enumeration as an Iterable object for use within a for each loop.- Type Parameters:
T
- the class type of the Enumeration elements.- Parameters:
enumeration
- the Enumeration to adapt as an Iterable object.- Returns:
- an Iterable instance of the Enumeration.
- See Also:
-
iterable
Adapts the given Iterator as an Iterable object for use within a for each loop. -
nullSafeCollection
Null-safe operation returning the givenCollection
if not null or an emptyCollection
(implemented withList
) if null.- Type Parameters:
T
- Class type of theCollection
elements.- Parameters:
collection
-Collection
to evaluate.- Returns:
- the given
Collection
if not null or return an emptyCollection
(implemented withList
). - See Also:
-
nullSafeEnumeration
Null-safe operation returning the givenEnumeration
if not null or anempty Enumeration
if null.- Type Parameters:
T
-type
of elements contained in theEnumeration
.- Parameters:
enumeration
-Enumeration
to evaluate.- Returns:
- the given
Enumeration
if not null or anempty Enumeration
. - See Also:
-
nullSafeIterable
A null-safe operation returning the original Iterable object if non-null or a default, empty Iterable implementation if null.- Type Parameters:
T
- the class type of the iterable elements.- Parameters:
iterable
- the Iterable object evaluated for a null reference.- Returns:
- the Iterable object if not null or a default, empty Iterable implementation otherwise.
- See Also:
-
nullSafeIterable
@Nullable public static <E,T extends Iterable<E>> T nullSafeIterable(@Nullable T iterable, @Nullable T defaultIterable) Returns the givenIterable
if not null or empty, otherwise returns thedefaultIterable
.- Type Parameters:
T
- concreteClass
type of theIterable
.E
-Class
type of the elements in theIterables
.- Parameters:
iterable
-Iterable
to evaluate.defaultIterable
-Iterable
to return if the giveniterable
is null or empty.- Returns:
iterable
if not null or empty otherwise returndefaultIterable
.- See Also:
-
nullSafeIterator
Null-safe operation returning the givenIterator
if not null or anempty Iterator
if null.- Type Parameters:
T
-type
of elements contained in theIterator
.- Parameters:
iterator
-Iterator
to evaluate.- Returns:
- the given
Iterator
if not null or anempty Iterator
. - See Also:
-
nullSafeList
-
nullSafeMap
-
nullSafeSet
-
nullSafeIsEmpty
Determines whether the givenCollection
isempty
.- Parameters:
collection
-Collection
to evaluate.- Returns:
- a boolean value indicating whether the given
Collection
isempty
. - See Also:
-
nullSafeIsEmpty
Determines whether the givenIterable
is empty. -
nullSafeIsEmpty
-
nullSafeSize
Determines theCollection.size()
of the givenCollection
.- Parameters:
collection
-Collection
to evaluate.- Returns:
- the
Collection.size()
of the givenCollection
. - See Also:
-
nullSafeSize
-
nullSafeSize
Determines theMap.size()
of the givenMap
.- Parameters:
map
-Map
to evaluate.- Returns:
- the
Map.size()
of the givenMap
. - See Also:
-
sort
Sors the elements of the givenList
by their natural,Comparable
ordering.- Type Parameters:
T
-Comparable
class type of the collection elements.- Parameters:
list
-List
of elements to sort.- Returns:
- the
List
sorted. - See Also:
-
subList
Returns a sub-list of elements from the givenList
based on the providedindices
.- Type Parameters:
T
- Class type of the elements in the list.- Parameters:
source
-List
from which the elements of the sub-list is constructed.indices
- array of indexes in thesource
List
to the elements used to construct the sub-list.- Returns:
- a sub-list of elements from the given
List
based on the providedindices
. - Throws:
IndexOutOfBoundsException
- if the array of indexes contains an index that is not within the bounds of the list.NullPointerException
- if either the list or indexes are null.- See Also:
-
toString
-