public abstract class CollectionUtils extends CollectionUtils
CollectionUtils is a utility class for working with the Java Collections Framework and classes.Collection,
Collections,
Enumeration,
Iterator,
List,
Map,
Set,
CollectionUtils| Constructor and Description |
|---|
CollectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <E,T extends Collection<E>> |
addAll(T collection,
Iterable<E> iterable)
Adds all elements from the given
Iterable to the Collection. |
static <T> Set<T> |
asSet(T... elements)
Returns an unmodifiable
Set containing the elements from the given object array. |
static boolean |
containsAny(Collection<?> collection,
Object... elements)
Null-safe method to determines whether the given
Collection contains any elements from the given array. |
static <T> Iterable<T> |
emptyIterable()
Returns an empty
Iterable 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> |
iterable(Iterator<T> iterator)
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 given
Collection if not null
or an empty Collection (implemented with List) if null. |
static <T> Enumeration<T> |
nullSafeEnumeration(Enumeration<T> enumeration)
Null-safe operation returning the given
Enumeration if not null
or an empty Enumeration if null. |
static boolean |
nullSafeIsEmpty(Collection<?> collection)
Determines whether the given
Collection is empty. |
static boolean |
nullSafeIsEmpty(Iterable<?> iterable)
Determines whether the given
Iterable is empty. |
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>> |
nullSafeIterable(T iterable,
T defaultIterable)
Returns the given
Iterable if not null or empty, otherwise returns the defaultIterable. |
static <T> Iterator<T> |
nullSafeIterator(Iterator<T> iterator)
Null-safe operation returning the given
Iterator if not null
or an empty 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 int |
nullSafeSize(Collection<?> collection)
Determines the
Collection.size() of the given Collection. |
static long |
nullSafeSize(Iterable iterable)
|
static int |
nullSafeSize(Map<?,?> map)
Determines the
Map.size() of the given Map. |
static <T extends Comparable<T>> |
sort(List<T> list)
Sors the elements of the given
List by their natural, Comparable ordering. |
static <T> List<T> |
subList(List<T> source,
int... indices)
Returns a sub-list of elements from the given
List based on the provided indices. |
static String |
toString(Map<?,?> map)
|
arrayToList, contains, contains, containsAny, containsInstance, findCommonElementType, findFirstMatch, findValueOfType, findValueOfType, firstElement, firstElement, hasUniqueObject, isEmpty, isEmpty, lastElement, lastElement, mergeArrayIntoCollection, mergePropertiesIntoMap, newHashMap, newLinkedHashMap, toArray, toIterator, toMultiValueMap, unmodifiableMultiValueMappublic static <E,T extends Collection<E>> T addAll(@NonNull T collection, @Nullable 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.IllegalArgumentException - if Collection is null.Iterable,
Collection@SafeVarargs public static <T> Set<T> asSet(@NonNull T... elements)
Set containing the elements from the given object array.public static boolean containsAny(@Nullable Collection<?> collection, @Nullable 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> Iterable<T> emptyIterable()
Iterable object.T - Class type of the elements in the Iterable.Iterable.Iterable,
nullSafeIterable(Iterable)@NonNull public static <T> Iterable<T> iterable(@Nullable 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> Iterable<T> iterable(@Nullable Iterator<T> iterator)
@NonNull public static <T> Collection<T> nullSafeCollection(@Nullable 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> Enumeration<T> nullSafeEnumeration(@Nullable 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> Iterable<T> nullSafeIterable(@Nullable 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 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> Iterator<T> nullSafeIterator(@Nullable 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(),
Iteratorpublic static boolean nullSafeIsEmpty(@Nullable Collection<?> collection)
Collection is empty.collection - Collection to evaluate.Collection is empty.nullSafeCollection(Collection),
Collection.isEmpty()public static boolean nullSafeIsEmpty(@Nullable Iterable<?> iterable)
Iterable is empty.iterable - Iterable to evaluate.Iterable is empty.Iterable,
nullSafeIterable(Iterable)public static boolean nullSafeIsEmpty(@Nullable Map<?,?> map)
map - Map to evaluate.Map is empty.nullSafeMap(Map),
Map.isEmpty()public static int nullSafeSize(@Nullable Collection<?> collection)
Collection.size() of the given Collection.collection - Collection to evaluate.Collection.size() of the given Collection.nullSafeCollection(Collection),
Collection.size()public static int nullSafeSize(@Nullable Map<?,?> map)
Map.size() of the given Map.map - Map to evaluate.Map.size() of the given Map.nullSafeMap(Map),
Map.size()public static <T extends Comparable<T>> List<T> sort(@NonNull 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),
Listpublic static <T> List<T> subList(@NonNull 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.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.ListCopyright © 2011–2021 Pivotal Software, Inc.. All rights reserved.