Package org.operaton.commons.utils
Class CollectionUtil
java.lang.Object
org.operaton.commons.utils.CollectionUtil
helper/convience methods for working with collections.
- Author:
- Joram Barrez
-
Method Summary
Modifier and TypeMethodDescriptionstatic <S,T> void addCollectionToMapOfSets(Map<S, Set<T>> map, S key, Collection<T> values) Adds a collection of values to a map of sets.static <S,T> void addToMapOfLists(Map<S, List<T>> map, S key, T value) Adds a value to a list associated with the specified key in a map of lists.static <S,T> void addToMapOfSets(Map<S, Set<T>> map, S key, T value) Adds a value to a map of sets.static <T> List<T> asArrayList(T[] values) Converts an array to anArrayList.static <T> Set<T> asHashSet(T... elements) Creates a newHashSetcontaining the specified elements.static <T> List<T> collectInList(Iterator<T> iterator) Converts an iterator into a list.static <T> TgetLastElement(Iterable<T> elements) Retrieves the last element from an iterable.static booleanhasElements(Collection<?> collection) Checks if a collection is notnulland contains elements.static booleanisEmpty(Collection<?> collection) Checks if a collection isnullor empty.static <S,T> void mergeMapsOfLists(Map<S, List<T>> map, Map<S, List<T>> toAdd) Merges two maps of lists.Chops a list into non-view sublists of length partitionSize.singletonMap(String key, Object value) Helper method that creates a singleton map.
-
Method Details
-
singletonMap
Helper method that creates a singleton map.This is an alternative to
Collections.singletonMap(Object, Object), which returns a generic typed mapMap<K,V>depending on the input type. This method specifically returns aMap<String, Object>which is commonly needed.- Parameters:
key- The key for the map.value- The value associated with the key.- Returns:
- A map containing a single entry with the specified key and value.
-
asArrayList
Converts an array to anArrayList.Arrays.asList cannot be reliably used for SQL parameters on MyBatis invalid input: '<' 3.3.0
- Type Parameters:
T- The type of elements in the array.- Parameters:
values- The array to be converted.- Returns:
- An
ArrayListcontaining the elements of the array. - Throws:
NullPointerException- if thevaluesparameter isnull
-
asHashSet
Creates a newHashSetcontaining the specified elements.- Type Parameters:
T- The type of elements.- Parameters:
elements- The elements to be added to the set.- Returns:
- A
HashSetcontaining the provided elements. - Throws:
NullPointerException- if theelementsparameter isnull
-
addToMapOfLists
Adds a value to a list associated with the specified key in a map of lists.If the key does not exist in the map, a new
ArrayListis created and associated with the key before adding the value.- Type Parameters:
S- The type of the key.T- The type of the value.- Parameters:
map- The map to which the value will be added.key- The key for the map entry.value- The value to be added to the list.- Throws:
UnsupportedOperationException- if the map is read-only
-
mergeMapsOfLists
Merges two maps of lists. Values from the second map are added to the first map.- Type Parameters:
S- The type of the key.T- The type of the value.- Parameters:
map- The map to which values will be added.toAdd- The map containing values to be merged.- Throws:
NullPointerException- if eithermapisnull, or iftoAddcontainsnulllistsUnsupportedOperationException- if the destination map is read-only
-
addToMapOfSets
Adds a value to a map of sets. If the key does not exist, a new set is created.- Type Parameters:
S- The type of the key.T- The type of the value.- Parameters:
map- The map to which the value will be added.key- The key for the map entry.value- The value to be added to the set.- Throws:
NullPointerException- ifmapisnullUnsupportedOperationException- if the destination map is read-only
-
addCollectionToMapOfSets
Adds a collection of values to a map of sets. If the key does not exist, a new set is created.- Type Parameters:
S- The type of the key.T- The type of the values.- Parameters:
map- The map to which the values will be added.key- The key for the map entry.values- The collection of values to be added to the set.- Throws:
NullPointerException- ifmapisnullUnsupportedOperationException- if the destination map is read-only
-
partition
Chops a list into non-view sublists of length partitionSize. Note: the argumentlistmay be included in the result.Note:
partitionSizemust be greater than 0- Type Parameters:
T- The type of elements in the list.- Parameters:
list- The list to be partitioned.partitionSize- The size of each partition.- Returns:
- A list of sublists, each of size partitionSize (except possibly the last one).
-
collectInList
Converts an iterator into a list.- Type Parameters:
T- The type of elements in the iterator.- Parameters:
iterator- The iterator to be converted.- Returns:
- A list containing all elements from the
iterator.
-
getLastElement
Retrieves the last element from an iterable. If the iterable is a list, the last element is accessed directly.- Type Parameters:
T- The type of elements in the iterable.- Parameters:
elements- The iterable from which the last element is retrieved.- Returns:
- The last element, or
nullif the iterable is empty.
-
isEmpty
Checks if a collection isnullor empty.- Parameters:
collection- The collection to check.- Returns:
- True if the collection is
nullor empty, false otherwise.
-
hasElements
Checks if a collection is notnulland contains elements.- Parameters:
collection- The collection to check.- Returns:
- True if the collection is not
nulland contains elements, false otherwise.
-