- java.lang.Object
-
- com.pervasive.datarush.commons.util.CollectionUtil
-
public final class CollectionUtil extends Object
Miscellaneous collection utilities.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K,V>
voidaddListMap(Map<K,List<V>> map, K key, V val)
Adds an element to a map of lists.static <T> void
clearListMap(Map<?,List<T>> map)
Clears all lists in a map of listsstatic byte[]
concat(byte[] array1, byte[] array2)
Concatenates two byte arrays into a single arraystatic <T> List<T>
concat(List<T> list1, List<T> list2)
Concatenates two lists into a single list.static <T> T[]
concat(T[] array1, T[] array2, Class<T> componentType)
Concatenates two arrays into a single array.static <T> List<T>
concatAsList(Collection<T> collection1, Collection<T> collection2)
Concatenates two collections and returns the result as a list.static <T> List<T>
copy(List<T> list)
Creates a copy containing the elements of the given list.static <K,V>
Map<K,V>firstValueMap(Map<K,List<V>> map)
Returns a map whose keys equal the keys of the given map and whose values equal the first value of each entry in the map.static <K,V>
List<V>getListMap(Map<K,List<V>> map, K key)
Returns a list from a map of lists or empty list if the element does not exist.static boolean
hasNulls(Collection<?> values)
Tests whether or not the provided collection contains null valuesstatic boolean
hasNullValues(Map<String,String> map)
Tests whether or not the provided map contains null valuesstatic <T> List<T>
newArrayList()
Syntactic sugar for ArrayList creation; avoids generic parameters on RHS.static <T> List<T>
newArrayList(int capacity)
Syntactic sugar for ArrayList creation; avoids generic parameters on RHS.static <K,V>
Map<K,V>newHashMap()
Syntactic sugar for HashMap creation; avoids generic parameters on RHS.static <K,V>
Map<K,V>newHashMap(int capacity)
Syntactic sugar for HashMap creation; avoids generic parameters on RHS.static <T> Set<T>
newHashSet()
Syntactic sugar for HashSet creation; avoids generic parameters on RHS.static <T> Set<T>
newHashSet(int capacity)
Syntactic sugar for HashSet creation; avoids generic parameters on RHS.static <K,V>
Map<K,V>newIdentityHashMap()
Syntactic sugar for IdentityHashMap creation; avoids generic parameters on RHS.static <K,V>
Map<K,V>newIdentityHashMap(int capacity)
Syntactic sugar for IdentityHashMap creation; avoids generic parameters on RHS.static <K,V>
Map<K,V>newLinkedHashMap()
Syntactic sugar for LinkedHashMap creation; avoids generic parameters on RHS.static <K,V>
Map<K,V>newLinkedHashMap(int capacity)
Syntactic sugar for LinkedHashMap creation; avoids generic parameters on RHS.static <T> Set<T>
newLinkedHashSet()
Syntactic sugar for LinkedHashSet creation; avoids generic parameters on RHS.static <T> Set<T>
newLinkedHashSet(int capacity)
Syntactic sugar for LinkedHashSet creation; avoids generic parameters on RHS.static <K,V>
SortedMap<K,V>newTreeMap()
Syntactic sugar for TreeMap creation; avoids generic parameters on RHS.static <T> T[]
prepend(T[] base, T element, Class<T> clazz)
Adds the given element to the start of the array, returning the new arraystatic <T> T[]
removeFirst(T[] base, Class<T> clazz)
Removes the first element of the array, returning the new arraystatic <T> List<T>
substract(List<T> list1, List<T> list2)
Computes a new list containing all of the elements of the first list, that are not in the second list.static <T> List<T>
substractAsList(Collection<T> collection1, Collection<T> collection2)
Computes a list containing all of the elements of the first collection, that are not in the second collection.static <T> T[]
toArray(Collection<? extends T> collection, Class<T> componentType)
Syntactic sugar for converting a collection to an array of a given component type.static int[]
toArray(List<Integer> vals)
Converts the list of integers into an array of intsstatic <K,V>
Map<K,List<V>>toMultiValueMap(Map<K,V> map)
Returns a map whose keys equal the keys in the given map and whose values equal singleton lists consisting of the values in the map.static boolean
unique(Collection<?> values)
Tests whether or not the provided collection contains only distinct valuesstatic <T> List<T>
unmodCopy(List<T> list)
Creates an unmodifiable copy of the provided liststatic <K,V>
Map<K,V>unmodCopy(Map<K,V> map)
Creates an unmodifiable copy of the provided map
-
-
-
Method Detail
-
newArrayList
public static <T> List<T> newArrayList()
Syntactic sugar for ArrayList creation; avoids generic parameters on RHS.- Type Parameters:
T
- the element type- Returns:
- an empty list
-
newArrayList
public static <T> List<T> newArrayList(int capacity)
Syntactic sugar for ArrayList creation; avoids generic parameters on RHS.- Type Parameters:
T
- the element type- Parameters:
capacity
- initial capacity for the list- Returns:
- an empty list
-
newHashMap
public static <K,V> Map<K,V> newHashMap()
Syntactic sugar for HashMap creation; avoids generic parameters on RHS.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- an empty map
-
newHashMap
public static <K,V> Map<K,V> newHashMap(int capacity)
Syntactic sugar for HashMap creation; avoids generic parameters on RHS.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
capacity
- initial capacity for the map- Returns:
- an empty map
-
newHashSet
public static <T> Set<T> newHashSet()
Syntactic sugar for HashSet creation; avoids generic parameters on RHS.- Type Parameters:
T
- the element type- Returns:
- an empty set
-
newHashSet
public static <T> Set<T> newHashSet(int capacity)
Syntactic sugar for HashSet creation; avoids generic parameters on RHS.- Type Parameters:
T
- the element type- Parameters:
capacity
- initial capacity for the map- Returns:
- an empty set
-
newTreeMap
public static <K,V> SortedMap<K,V> newTreeMap()
Syntactic sugar for TreeMap creation; avoids generic parameters on RHS.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- an empty map
-
newIdentityHashMap
public static <K,V> Map<K,V> newIdentityHashMap()
Syntactic sugar for IdentityHashMap creation; avoids generic parameters on RHS.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- an empty map
-
newIdentityHashMap
public static <K,V> Map<K,V> newIdentityHashMap(int capacity)
Syntactic sugar for IdentityHashMap creation; avoids generic parameters on RHS.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
capacity
- initial capacity for the map- Returns:
- an empty map
-
newLinkedHashMap
public static <K,V> Map<K,V> newLinkedHashMap()
Syntactic sugar for LinkedHashMap creation; avoids generic parameters on RHS.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- an empty map
-
newLinkedHashMap
public static <K,V> Map<K,V> newLinkedHashMap(int capacity)
Syntactic sugar for LinkedHashMap creation; avoids generic parameters on RHS.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
capacity
- initial capacity for the map- Returns:
- an empty map
-
newLinkedHashSet
public static <T> Set<T> newLinkedHashSet()
Syntactic sugar for LinkedHashSet creation; avoids generic parameters on RHS.- Type Parameters:
T
- the element type- Returns:
- an empty set
-
newLinkedHashSet
public static <T> Set<T> newLinkedHashSet(int capacity)
Syntactic sugar for LinkedHashSet creation; avoids generic parameters on RHS.- Type Parameters:
T
- the element type- Parameters:
capacity
- initial capacity for the map- Returns:
- an empty map
-
toArray
public static <T> T[] toArray(Collection<? extends T> collection, Class<T> componentType)
Syntactic sugar for converting a collection to an array of a given component type.- Type Parameters:
T
- the element type- Parameters:
collection
- The collection.componentType
- The component type of the array- Returns:
- An array consisting of the elements of the collection
-
concat
public static <T> List<T> concat(List<T> list1, List<T> list2)
Concatenates two lists into a single list.- Type Parameters:
T
- the element type- Parameters:
list1
- the first listlist2
- the second list- Returns:
- a list consisting of the elements of the first list followed by the elements of the second
-
concatAsList
public static <T> List<T> concatAsList(Collection<T> collection1, Collection<T> collection2)
Concatenates two collections and returns the result as a list.- Parameters:
collection1
- the first collectioncollection2
- the second collection- Returns:
- a list consisting of the elements of the first collection followed by the elements of the second
-
concat
public static <T> T[] concat(T[] array1, T[] array2, Class<T> componentType)
Concatenates two arrays into a single array.- Type Parameters:
T
- the element type- Parameters:
array1
- the first arrayarray2
- the second arraycomponentType
- the class of elements in the resulting array- Returns:
- an array consisting of the elements of the first array followed by the elements of the second
-
concat
public static byte[] concat(byte[] array1, byte[] array2)
Concatenates two byte arrays into a single array- Parameters:
array1
- the first byte arrayarray2
- the second byte array- Returns:
- an array consisting of the bytes of the first array followed by the bytes of the second
-
prepend
public static <T> T[] prepend(T[] base, T element, Class<T> clazz)
Adds the given element to the start of the array, returning the new array- Type Parameters:
T
- the element type- Parameters:
base
- the initial arrayelement
- the element to prependclazz
- the type of the resulting array- Returns:
- the array consisting of the specified element followed by all the elements from the base array
-
substract
public static <T> List<T> substract(List<T> list1, List<T> list2)
Computes a new list containing all of the elements of the first list, that are not in the second list.- Parameters:
list1
- the first listlist2
- the second list- Returns:
- a new list containing the elements of the first list, that are not in the second list.
-
substractAsList
public static <T> List<T> substractAsList(Collection<T> collection1, Collection<T> collection2)
Computes a list containing all of the elements of the first collection, that are not in the second collection.- Parameters:
collection1
- the first collectioncollection2
- the second collection- Returns:
- a new list containing the elements of the first collection, that are not in the second collection.
-
copy
public static <T> List<T> copy(List<T> list)
Creates a copy containing the elements of the given list.- Parameters:
list
- the list to be copied- Returns:
- a copy of the list
-
removeFirst
public static <T> T[] removeFirst(T[] base, Class<T> clazz)
Removes the first element of the array, returning the new array- Type Parameters:
T
- the element type- Parameters:
base
- the initial arrayclazz
- the type of the resulting array- Returns:
- the array consisting of all the elements in the base array excepting the first
-
clearListMap
public static <T> void clearListMap(Map<?,List<T>> map)
Clears all lists in a map of lists- Type Parameters:
T
- the type of list- Parameters:
map
- a map of lists
-
addListMap
public static <K,V> void addListMap(Map<K,List<V>> map, K key, V val)
Adds an element to a map of lists.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
map
- the map of listskey
- the keyval
- the value
-
firstValueMap
public static <K,V> Map<K,V> firstValueMap(Map<K,List<V>> map)
Returns a map whose keys equal the keys of the given map and whose values equal the first value of each entry in the map.- Parameters:
map
- the map- Returns:
- a new map
-
toMultiValueMap
public static <K,V> Map<K,List<V>> toMultiValueMap(Map<K,V> map)
Returns a map whose keys equal the keys in the given map and whose values equal singleton lists consisting of the values in the map. In the case where the value in the original map is null, the value in the new map will be the empty list.- Parameters:
map
- the original map- Returns:
- a new map
-
getListMap
public static <K,V> List<V> getListMap(Map<K,List<V>> map, K key)
Returns a list from a map of lists or empty list if the element does not exist. This method will never return null.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
map
- the map of listskey
- the key- Returns:
- a list or empty if the key does not exist. Returned list will never be null and will always be immutable
-
toArray
public static int[] toArray(List<Integer> vals)
Converts the list of integers into an array of ints- Parameters:
vals
- the integers- Returns:
- an array of ints
- Throws:
NullPointerException
- if the given list contains any null values
-
unique
public static boolean unique(Collection<?> values)
Tests whether or not the provided collection contains only distinct values- Parameters:
values
- the collection to be tested- Returns:
- true, if the collection contains only distinct values, false otherwise
-
hasNulls
public static boolean hasNulls(Collection<?> values)
Tests whether or not the provided collection contains null values- Parameters:
values
- the collection to be tested- Returns:
- true, if the collection contains null values, false otherwise
-
hasNullValues
public static boolean hasNullValues(Map<String,String> map)
Tests whether or not the provided map contains null values- Parameters:
values
- the map to be tested- Returns:
- true, if the map contains null values, false otherwise
-
unmodCopy
public static <T> List<T> unmodCopy(List<T> list)
Creates an unmodifiable copy of the provided list- Parameters:
list
- the list to be copied- Returns:
- unmodifiable copy of the provided list
-
-