public final class CollectionUtil extends Object
Modifier and Type | Method and Description |
---|---|
static <T> List<T> |
adaptedList(Object object)
Adapts any object to a new non-null mutable List, regardless of the
nature of the object.
|
static <T> Set<T> |
adaptedSet(Object object)
Adapts any object to a new non-null mutable Set, regardless of the
nature of the object.
|
static <T> List<T> |
asListOrEmpty(T... values)
Returns a fixed-size list backed by the specified array or
an empty list if the array is
null . |
static <T> List<T> |
asListOrNull(T... values)
Returns a fixed-size list backed by the specified array or
null if the array is null . |
static void |
blanksToNulls(Collection<String> c)
Convert blank string entries to
null . |
static void |
emptiesToNulls(Collection<String> c)
Convert empty string entries to
null . |
static void |
nullsToEmpties(Collection<String> c)
Convert
null entries to empty strings. |
static void |
removeBlanks(Collection<String> c)
Removes blank strings in the given collection.
|
static void |
removeEmpties(Collection<String> c)
Removes empty strings in the given collection.
|
static void |
removeNulls(Collection<?> c)
Removes
null entries in the given collection. |
static <T> void |
replaceAll(Collection<T> c,
T source,
T target)
Replaces all elements matching the source value with the target
value.
|
static <T> void |
setAll(Collection<T> target,
Collection<T> source)
Sets all values of the source collection into the target one.
|
static <T> void |
setAll(Collection<T> target,
T... source)
Sets all values of the source array into the target collection.
|
static <K,V> void |
setAll(Map<K,V> target,
Map<K,V> source)
Sets all values of the source map into the target one.
|
static Object |
toArray(Collection<?> c,
Class<?> arrayType)
Converts a collection to an array using the specified type.
|
static List<String> |
toStringList(Collection<?> values)
Converts a list of objects to a list of strings using
default
Converter instance. |
static List<String> |
toStringList(Object... values)
Converts a list of objects to a list of strings using
default
Converter instance. |
static <T> List<T> |
toTypeList(List<String> values,
Class<T> targetClass)
Converts a list of strings to a list of objects matching
the return type.
|
static <T> List<T> |
toTypeList(List<String> values,
Function<String,T> converter)
Converts a list of strings to a list of objects matching
the return type.
|
static <T> List<T> |
unmodifiableList(T... values)
Returns an unmodifiable view of the specified list.
|
static <T> Set<T> |
unmodifiableSet(T... values)
Returns an unmodifiable view of the specified set.
|
public static <T> List<T> adaptedList(Object object)
null
or a blank string,
an empty list is returned.
The supplied object must be a value or values of the expected type.T
- the expected return type of the listobject
- object to adaptpublic static <T> Set<T> adaptedSet(Object object)
null
or a blank string,
an empty set is returned.
The supplied object must be a value or values of the expected type.T
- the expected return type of the setobject
- object to adaptpublic static Object toArray(Collection<?> c, Class<?> arrayType)
c
- collection to convertarrayType
- array typepublic static <T> void setAll(Collection<T> target, Collection<T> source)
null
and will not clear/add if the source
collection is the same instance as the target one.
If target is null
, invoking this method has no effect.
If source is null
will clear the target collectionT
- objects class typetarget
- target collectionsource
- source collection@SafeVarargs public static <T> void setAll(Collection<T> target, T... source)
null
.
If target is null
, invoking this method has no effect.
If source is null
or is an array containing only one
null
element, it will clear the target collectionT
- objects class typetarget
- target collectionsource
- source collectionpublic static <K,V> void setAll(Map<K,V> target, Map<K,V> source)
null
and will not clear/add if the source
map is the same instance as the target one.
If target is null
, invoking this method has no effect.
If source is null
will clear the target mapK
- key typeV
- value typetarget
- target mapsource
- source map@SafeVarargs public static <T> List<T> asListOrEmpty(T... values)
null
. This is a null-safe
version of Arrays.asList(Object...)
.T
- objects class typevalues
- the array by which the list will be backedasListOrNull(Object...)
@SafeVarargs public static <T> List<T> asListOrNull(T... values)
null
if the array is null
. This is a null-safe
version of Arrays.asList(Object...)
.
A list is returned even if the array is empty or contains only
null
values.T
- objects class typevalues
- the array by which the list will be backedasListOrEmpty(Object...)
public static List<String> toStringList(Object... values)
Converter
instance.
If the supplied list is null
, an empty string list
is returned.values
- list to convert to a list of stringspublic static List<String> toStringList(Collection<?> values)
Converter
instance.
If the supplied list is null
, an empty string list
is returned.values
- list to convert to a list of stringspublic static <T> List<T> toTypeList(List<String> values, Class<T> targetClass)
null
, an empty list
is returned.T
- objects class typevalues
- list to convert to a list of the given typetargetClass
- target classpublic static <T> List<T> toTypeList(List<String> values, Function<String,T> converter)
null
, an empty list
is returned.T
- target objects class typevalues
- list to convert to a list of the given typeconverter
- function converting string to typepublic static <T> List<T> unmodifiableList(T... values)
Collections.unmodifiableList(List)
.T
- target objects class typevalues
- the values to convert to an unmodifiable listpublic static <T> Set<T> unmodifiableSet(T... values)
Collections.unmodifiableSet(Set)
.
This method uses a LinkedHashSet
to maintain order.T
- target objects class typevalues
- the values to convert to an unmodifiable listpublic static void removeNulls(Collection<?> c)
null
entries in the given collection. Only
useful for collection implementations allowing null
entries.c
- a collectionpublic static void removeBlanks(Collection<String> c)
c
- a string collectionpublic static void removeEmpties(Collection<String> c)
c
- a string collectionpublic static <T> void replaceAll(Collection<T> c, T source, T target)
T
- elements typec
- a collectionsource
- object to replacetarget
- replacementpublic static void nullsToEmpties(Collection<String> c)
null
entries to empty strings.c
- a string collectionpublic static void emptiesToNulls(Collection<String> c)
null
.c
- a string collectionpublic static void blanksToNulls(Collection<String> c)
null
.c
- a string collectionCopyright © 2008–2023 Norconex Inc.. All rights reserved.