Package util
Class CollectionUtils
java.lang.Object
util.CollectionUtils
A collection of utility methods that prevent you from having to do unsafe casts of
Collection
classes due to runtime type erasure.
Be sure to check Apache collection utils before using this class, as it's a standard utility and often more efficient.
Some examples:
CollectionUtils
IterableUtils
IteratorUtils
StringUtils.join(Iterable, char)
- for pretty printing collections with newlinesApache CollectionUtils.collect(Collection, Transformer)
- to turn a collection in to collection of strings when the defaulttoString()
is lacking
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Returns an element from the given iterable; null if the iterable is null or empty.static <T> T
any
(Collection<T> c) Returns an element from the given collection; null if the collection is null or empty.static <T> Iterable
<T> asCancellableIterable
(TaskMonitor monitor, Iterable<T>... iterables) Combines all collections passed-in into a pass-through (not creating a new collection) Iterable that uses the given task monitor.static <T> Collection
<T> asCollection
(Collection<?> collection, Class<T> clazz) Checks that the elements in the given collection are of the type specified byclazz
and then casts the collection to be of the specified type.static <T> Collection
<T> asCollection
(Collection<T> c) Returns the given collection if not null, an empty collection otherwise.static <T> Iterable
<T> asIterable
(Iterable<T>... iterables) Combines all collections passed-in into a pass-through (not creating a new collection) Iterable.static <T> Iterable
<T> asIterable
(Iterator<T> iterator) Returns an iterable over an iteratorstatic <T> Iterable
<T> asIterable
(T t) Turns the given item into an iterablestatic <T> List
<T> static <T> List
<T> asList
(Collection<T> c) A convenient way to check for null and whether the given collection is aList
.static <T> List
<T> asList
(Enumeration<T> enumeration) static <T> List
<T> static <T> List
<T> Checks that the elements in the given list are of the type specified byclazz
and then casts the list to be of the specified type.static <T> List
<T> Returns the given list if not null, otherwise returns an empty list.static <T> List
<T> asList
(T... items) Similar toArrays.asList(Object...)
, except that this method will turn a single null parameter into an empty list.static <T> Set
<T> Turns the given iterable into a new Set, returning it directly if it is a set, draining it into a set if it is not already.static <T> Set
<T> asSet
(Collection<T> c) static <T> Set
<T> Drains the given iterator into a new Setstatic <T> Set
<T> asSet
(T... items) Turns the given items into a set.static <T> Stream
<T> Combines all iterables passed-in into a pass-through (not creating a new collection) Stream.static <T> Stream
<T> Turns the given iterator into a streamstatic boolean
Returns true if all the given objects are null.static <T> boolean
isAllNull
(Collection<T> c) Returns true if all the given objects are null.static <T> boolean
isAllSameType
(Collection<?> list, Class<T> clazz) Returns true if each item in the list is of type clazz.static <T> boolean
isBlank
(Collection<T> c) Returns true if the given array is null or has 0 lengthstatic <T> boolean
isBlank
(T... t) Returns true if the given array is null or has 0 lengthstatic <T> boolean
isOneOf
(T t, T... possibles) Returns true if the given item is in the collection of possible itemsstatic <T> Collection
<T> nonNull
(Collection<T> c) Returns the given collection if not null, an empty collection (a Set) otherwise.
-
Method Details
-
asSet
Turns the given items into a set. If there is only a single item and it is null, then an empty set will be returned.- Parameters:
items
- the items to put in the set- Returns:
- the list of items
-
asSet
-
asSet
Drains the given iterator into a new Set- Parameters:
it
- the iterator- Returns:
- the set
-
asSet
Turns the given iterable into a new Set, returning it directly if it is a set, draining it into a set if it is not already.- Parameters:
iterable
- the iterable- Returns:
- the set
-
asList
Similar toArrays.asList(Object...)
, except that this method will turn a single null parameter into an empty list. Also, this method creates a new, mutable array, whereas the former's array is not mutable.- Parameters:
items
- the items to add to the list- Returns:
- the list
-
asList
Returns the given list if not null, otherwise returns an empty list. This is useful for clients avoid null checks.- Parameters:
list
- the list to check- Returns:
- a non-null collection
-
asList
A convenient way to check for null and whether the given collection is aList
. If the value is a list, then it is returned. If the value is null, an empty list is returned. Otherwise, a new list is created from the given collection.- Parameters:
c
- the collection to check- Returns:
- a list
-
nonNull
Returns the given collection if not null, an empty collection (a Set) otherwise. This is useful for clients avoid null checks.- Parameters:
c
- the collection to check- Returns:
- a non-null collection
-
asCollection
Returns the given collection if not null, an empty collection otherwise. This is useful for clients avoid null checks.- Parameters:
c
- the collection to check- Returns:
- a non-null collection
-
asList
-
asList
-
asList
-
asList
Checks that the elements in the given list are of the type specified byclazz
and then casts the list to be of the specified type.- Parameters:
list
- the source listclazz
- the class of T- Returns:
- a casted list of type T
- Throws:
IllegalArgumentException
- if the given list contains elements that are not of the type specified byclazz
.
-
asCollection
Checks that the elements in the given collection are of the type specified byclazz
and then casts the collection to be of the specified type.- Parameters:
collection
- the source collectionclazz
- the class of T- Returns:
- a casted list of type T
- Throws:
IllegalArgumentException
- if the given collection contains elements that are not of the type specified byclazz
.
-
isAllSameType
Returns true if each item in the list is of type clazz.- Type Parameters:
T
- the type- Parameters:
list
- the list to inspectclazz
- the class type- Returns:
- true if each item in the list is of type clazz
-
isOneOf
Returns true if the given item is in the collection of possible items- Parameters:
t
- the item in questionpossibles
- the set of things- Returns:
- true if the given item is in the collection of possible items
-
isAllNull
Returns true if all the given objects are null.See also apache
ObjectUtils.anyNotNull(Object...)
andObjectUtils.allNotNull(Object...)
- Parameters:
objects
- the objects to check- Returns:
- true if all the given objects are null
-
isAllNull
Returns true if all the given objects are null.See also apache
ObjectUtils.anyNotNull(Object...)
andObjectUtils.allNotNull(Object...)
- Parameters:
c
- the objects to check- Returns:
- true if all the given objects are null
-
isBlank
Returns true if the given array is null or has 0 length- Parameters:
c
- the collection to check- Returns:
- true if blank
-
isBlank
public static <T> boolean isBlank(T... t) Returns true if the given array is null or has 0 length- Parameters:
t
- the items to check- Returns:
- true if blank
-
asIterable
Turns the given item into an iterable- Parameters:
t
- the object from which to create an iterable- Returns:
- an iterable over the given iterator
-
asIterable
Returns an iterable over an iterator- Parameters:
iterator
- the iterator to create an iterable from- Returns:
- an iterable over the given iterator
-
asIterable
Combines all collections passed-in into a pass-through (not creating a new collection) Iterable.- Parameters:
iterables
- the iterables to combine- Returns:
- the iterable
-
asCancellableIterable
@SafeVarargs public static <T> Iterable<T> asCancellableIterable(TaskMonitor monitor, Iterable<T>... iterables) Combines all collections passed-in into a pass-through (not creating a new collection) Iterable that uses the given task monitor.- Parameters:
monitor
- a task monitor that allows for cancelling iterationiterables
- the iterables to combine- Returns:
- the iterable
-
asStream
Turns the given iterator into a stream- Parameters:
iterator
- the iterator- Returns:
- the stream
-
asStream
Combines all iterables passed-in into a pass-through (not creating a new collection) Stream.- Parameters:
iterables
- the iterables to combine- Returns:
- the stream
-
any
Returns an element from the given collection; null if the collection is null or empty. This is meant for clients that have a collection with any number of items and just need to get one.- Parameters:
c
- the collection- Returns:
- the item
-
any
Returns an element from the given iterable; null if the iterable is null or empty. This is meant for clients that have a collection with any number of items and just need to get one.- Parameters:
iterable
- the items- Returns:
- the item
-