Class CollectionUtils
java.lang.Object
org.deltava.util.CollectionUtils
A utility class for dealing with Collections.
- Since:
- 1.0
- Version:
- 10.2
- Author:
- Luke
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> void addMapCollection
(Map<K, Collection<V>> m, K key, V entry) Adds an entry to a Collection contained as a Map value.static <K,
V> void addMapCollection
(Map<K, Collection<V>> m, K key, V entry, Supplier<Collection<V>> cf) Adds an entry to a Collection contained as a Map value, allowing a specific Collection constructor.static <K,
V> Map <K, V> createMap
(Collection<K> keys, Collection<V> values) Converts two Collections into a Map.static <K,
V> Map <K, V> createMap
(Collection<V> values, Function<V, K> f) Converts a Collection into a Map.static <T> Collection
<T> getDelta
(Collection<T> c1, Collection<T> c2) Determines which elements are contained within only one Collection.static <T> boolean
hasDelta
(Collection<T> c1, Collection<T> c2) Compares two Collections to see if there are any differences between them.static boolean
isEmpty
(Collection<?> c) A null-safe mechanism to determine if a collection has no data.static <T> Collection
<T> join
(Collection<T>... data) Joins multiple Collections into a single Collection without duplicates.static <T> Collection
<T> nonNull
(Supplier<Collection<T>> cf, T... values) Creates a Collection of non-null values.static <T> Collection
<T> nonNull
(T... values) Creates a Collection of non-null values.static <T> List
<T> sort
(Collection<T> c, Comparator<T> cmp) Clones and sorts a Collection using a particular Comparator.static <T> Collection
<T> union
(Collection<T> c1, Collection<T> c2) Examines two Collections and returns the items present in both.
-
Method Details
-
isEmpty
A null-safe mechanism to determine if a collection has no data.- Parameters:
c
- the Collection to check- Returns:
- TRUE if c is null or has no elements, otherwise FALSE
-
getDelta
Determines which elements are contained within only one Collection.- Parameters:
c1
- the Collection to examinec2
- the entries to check- Returns:
- a List of entries contained within c1, but not c2
- Throws:
NullPointerException
- if c1 or c2 are null
-
hasDelta
Compares two Collections to see if there are any differences between them. This is done by comparing their sizes, and then calling c1.containsAll(c2) to ensure that all of c2 is contained within c1.- Parameters:
c1
- the first Collection of entriesc2
- the second Collection of entries- Returns:
- TRUE if c1.size() != c2.size() or !c1.containsAll(c2), otherwise FALSE
-
join
Joins multiple Collections into a single Collection without duplicates.- Parameters:
data
- the Collections to join- Returns:
- a Collection of unique results
-
createMap
Converts a Collection into a Map.- Parameters:
values
- the values to usef
- the function to call on each value to get the key value- Returns:
- a Map of the values, indexed by their key
-
createMap
Converts two Collections into a Map. The resulting Map will be the same size as the smallest of the two supplied Collections, and the key/value pairs will be assigned in the normal iteration order of each Collection.- Parameters:
keys
- a Collection of key objectsvalues
- a Collection of value objects- Returns:
- a Map of key/value pairs.
-
sort
Clones and sorts a Collection using a particular Comparator.- Parameters:
c
- the Collection to sortcmp
- the Comparator- Returns:
- a sorted Collection
-
union
Examines two Collections and returns the items present in both.- Parameters:
c1
- the first Collectionc2
- the second Collection- Returns:
- a Collection of items present in both c1 and c2
-
addMapCollection
Adds an entry to a Collection contained as a Map value.- Parameters:
m
- the Mapkey
- the keyentry
- the value
-
addMapCollection
public static <K,V> void addMapCollection(Map<K, Collection<V>> m, K key, V entry, Supplier<Collection<V>> cf) Adds an entry to a Collection contained as a Map value, allowing a specific Collection constructor.- Parameters:
m
- the Mapkey
- the keyentry
- the valuecf
- the CollectionSupplier
-
nonNull
Creates a Collection of non-null values.- Parameters:
values
- the values to add- Returns:
- a Collection of non-null values
-
nonNull
Creates a Collection of non-null values.- Parameters:
cf
- the Collection factory functionvalues
- the values to add- Returns:
- a Collection of non-null values
-