Class CollectionUtil

java.lang.Object
org.operaton.commons.utils.CollectionUtil

public final class CollectionUtil extends Object
helper/convience methods for working with collections.
Author:
Joram Barrez
  • Method Details

    • singletonMap

      public static Map<String,Object> singletonMap(String key, Object value)
      Helper method that creates a singleton map.

      This is an alternative to Collections.singletonMap(Object, Object), which returns a generic typed map Map<K,V> depending on the input type. This method specifically returns a Map<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

      public static <T> List<T> asArrayList(T[] values)
      Converts an array to an ArrayList.

      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 ArrayList containing the elements of the array.
      Throws:
      NullPointerException - if the values parameter is null
    • asHashSet

      public static <T> Set<T> asHashSet(T... elements)
      Creates a new HashSet containing the specified elements.
      Type Parameters:
      T - The type of elements.
      Parameters:
      elements - The elements to be added to the set.
      Returns:
      A HashSet containing the provided elements.
      Throws:
      NullPointerException - if the elements parameter is null
    • addToMapOfLists

      public 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.

      If the key does not exist in the map, a new ArrayList is 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

      public static <S, T> void mergeMapsOfLists(Map<S,List<T>> map, Map<S,List<T>> toAdd)
      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 either map is null, or if toAdd contains null lists
      UnsupportedOperationException - if the destination map is read-only
    • addToMapOfSets

      public static <S, T> void addToMapOfSets(Map<S,Set<T>> map, S key, T value)
      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 - if map is null
      UnsupportedOperationException - if the destination map is read-only
    • addCollectionToMapOfSets

      public static <S, T> void addCollectionToMapOfSets(Map<S,Set<T>> map, S key, Collection<T> values)
      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 - if map is null
      UnsupportedOperationException - if the destination map is read-only
    • partition

      public static <T> List<List<T>> partition(List<T> list, int partitionSize)
      Chops a list into non-view sublists of length partitionSize. Note: the argument list may be included in the result.

      Note: partitionSize must 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

      public static <T> List<T> collectInList(Iterator<T> iterator)
      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

      public static <T> T getLastElement(Iterable<T> elements)
      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 null if the iterable is empty.
    • isEmpty

      public static boolean isEmpty(Collection<?> collection)
      Checks if a collection is null or empty.
      Parameters:
      collection - The collection to check.
      Returns:
      True if the collection is null or empty, false otherwise.
    • hasElements

      public static boolean hasElements(Collection<?> collection)
      Checks if a collection is not null and contains elements.
      Parameters:
      collection - The collection to check.
      Returns:
      True if the collection is not null and contains elements, false otherwise.