Class CollectionUtil
java.lang.Object
com.norconex.commons.lang.collection.CollectionUtil
Collection-related utility methods.
- Since:
- 2.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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 isnull.static <T> List<T> asListOrNull(T... values) Returns a fixed-size list backed by the specified array ornullif the array isnull.static voidConvert blank string entries tonull.static voidConvert empty string entries tonull.static voidConvertnullentries to empty strings.static voidRemoves blank strings in the given collection.static voidRemoves empty strings in the given collection.static voidremoveNulls(Collection<?> c) Removesnullentries in the given collection.static <T> voidreplaceAll(Collection<T> c, T source, T target) Replaces all elements matching the source value with the target value.static <T> voidsetAll(Collection<T> target, Collection<T> source) Sets all values of the source collection into the target one.static <T> voidsetAll(Collection<T> target, T... source) Sets all values of the source array into the target collection.static <K,V> void Sets all values of the source map into the target one.static ObjecttoArray(Collection<?> c, @NonNull Class<?> arrayType) Converts a collection to an array using the specified type (nevernull).toStringList(Object... values) Converts a list of objects to an unmodifiable list of strings using defaultGenericConverterinstance.toStringList(Collection<?> values) Converts a list of objects to an unmodifiable list of strings using defaultGenericConverterinstance.static <T> List<T> toTypeList(List<String> values, Class<T> targetClass) Converts a list of strings to an unmodifiable 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 an unmodifiable list of objects matching the return type.static <T> List<T> Create a non-null mutable list containing the combination of supplied objects of the specified type.static <T> Set<T> Create a non-null mutable set containing the combination of supplied objects of the specified type (minus duplicates).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.
-
Method Details
-
adaptedList
Adapts any object to a new non-null mutable List, regardless of the nature of the object. If the object is a Collection or array, it will be iterated over to populate the new list. If it is any other object, it will become the first and only item in the newly created list. If the object isnullor a blank string, an empty list is returned. The supplied object must be a value or values of the expected type.- Type Parameters:
T- the expected return type of the list- Parameters:
object- object to adapt- Returns:
- adapted list
-
adaptedSet
Adapts any object to a new non-null mutable Set, regardless of the nature of the object. If the object is a Collection or array, it will be iterated over to populate the new set. If it is any other object, it will become the first and only item in the newly created set. If the object isnullor a blank string, an empty set is returned. The supplied object must be a value or values of the expected type.- Type Parameters:
T- the expected return type of the set- Parameters:
object- object to adapt- Returns:
- adapted set
-
toArray
Converts a collection to an array using the specified type (never
null).Since 3.0.0, passing a
nullcollection argument will will return an empty array of the supplied type.- Parameters:
c- collection to convert ornullarrayType- array type (must not benull)- Returns:
- the new array (never
null) - Throws:
NullPointerException- if arrayType isnull
-
setAll
Sets all values of the source collection into the target one. Same as doing a "clear", followed by "addAll", but it also checks fornulland will not clear/add if the source collection is the same instance as the target one. If target isnull, invoking this method has no effect. If source isnullwill clear the target collection- Type Parameters:
T- objects class type- Parameters:
target- target collectionsource- source collection
-
setAll
Sets all values of the source array into the target collection. Same as doing a "clear", followed by "addAll" after converting the array to a list. It also checks fornull. If target isnull, invoking this method has no effect. If source isnullor is an array containing only onenullelement, it will clear the target collection- Type Parameters:
T- objects class type- Parameters:
target- target collectionsource- source collection
-
setAll
Sets all values of the source map into the target one. Same as doing a "clear", followed by "putAll", but it also checks fornulland will not clear/add if the source map is the same instance as the target one. If target isnull, invoking this method has no effect. If source isnullwill clear the target map- Type Parameters:
K- key typeV- value type- Parameters:
target- target mapsource- source map
-
asListOrEmpty
Returns a fixed-size list backed by the specified array or an empty list if the array isnull. This is a null-safe version ofArrays.asList(Object...).- Type Parameters:
T- objects class type- Parameters:
values- the array by which the list will be backed- Returns:
- a list view of the specified array (never
null) - See Also:
-
asListOrNull
Returns a fixed-size list backed by the specified array ornullif the array isnull. This is a null-safe version ofArrays.asList(Object...). A list is returned even if the array is empty or contains onlynullvalues.- Type Parameters:
T- objects class type- Parameters:
values- the array by which the list will be backed- Returns:
- a list view of the specified array
- See Also:
-
toStringList
Converts a list of objects to an unmodifiable list of strings using defaultGenericConverterinstance. If the supplied list isnull, an empty string list is returned.- Parameters:
values- list to convert to a list of strings- Returns:
- list of strings
-
toStringList
Converts a list of objects to an unmodifiable list of strings using defaultGenericConverterinstance. If the supplied list isnull, an empty string list is returned.- Parameters:
values- list to convert to a list of strings- Returns:
- list of strings
-
toTypeList
Converts a list of strings to an unmodifiable list of objects matching the return type. If the supplied list isnull, an empty list is returned.- Type Parameters:
T- objects class type- Parameters:
values- list to convert to a list of the given typetargetClass- target class- Returns:
- list
-
toTypeList
Converts a list of strings to an unmodifiable list of objects matching the return type. If the supplied list isnull, an empty list is returned.- Type Parameters:
T- target objects class type- Parameters:
values- list to convert to a list of the given typeconverter- function converting string to type- Returns:
- list
-
unmodifiableList
Returns an unmodifiable view of the specified list. Convenience method for doing with an array the same asCollections.unmodifiableList(List). Anullarray argument will return an empty list.- Type Parameters:
T- target objects class type- Parameters:
values- the values to convert to an unmodifiable list- Returns:
- unmodifiable list (never
null)
-
unmodifiableSet
Returns an unmodifiable view of the specified set. Convenience method for doing with an array the same asCollections.unmodifiableSet(Set). Anullarray argument will return an empty set. This method uses aLinkedHashSetto maintain order.- Type Parameters:
T- target objects class type- Parameters:
values- the values to convert to an unmodifiable list- Returns:
- unmodifiable list
-
removeNulls
Removesnullentries in the given collection. Only useful for collection implementations allowingnullentries.- Parameters:
c- a collection
-
removeBlanks
Removes blank strings in the given collection.- Parameters:
c- a string collection
-
removeEmpties
Removes empty strings in the given collection.- Parameters:
c- a string collection
-
replaceAll
Replaces all elements matching the source value with the target value.- Type Parameters:
T- elements type- Parameters:
c- a collectionsource- object to replacetarget- replacement
-
nullsToEmpties
Convertnullentries to empty strings.- Parameters:
c- a string collection
-
emptiesToNulls
Convert empty string entries tonull.- Parameters:
c- a string collection
-
blanksToNulls
Convert blank string entries tonull.- Parameters:
c- a string collection
-
unionList
Create a non-null mutable list containing the combination of supplied objects of the specified type. If the object is a Collection or array, it will be iterated over to populate the new list (recursively). Anynullobjects are ignored (not added to the returned list). Objects are returned in the order supplied.- Parameters:
objects- objects to combine into a new list- Returns:
- a new non-null, mutable list
- Since:
- 3.0.0
-
unionSet
Create a non-null mutable set containing the combination of supplied objects of the specified type (minus duplicates). If the object is a Collection or array, it will be iterated over to populate the new set (recursively). Anynullobjects are ignored (not added to the returned set). There are no guarantee on the order in which objects are returned.- Parameters:
objects- objects to combine into a new set- Returns:
- a new non-null, mutable set
- Since:
- 3.0.0
-