Class CollectionUtils

java.lang.Object
org.deltava.util.CollectionUtils

public class CollectionUtils extends Object
A utility class for dealing with Collections.
Since:
1.0
Version:
10.2
Author:
Luke
  • Method Details

    • isEmpty

      public static boolean isEmpty(Collection<?> c)
      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

      public static <T> Collection<T> getDelta(Collection<T> c1, Collection<T> c2)
      Determines which elements are contained within only one Collection.
      Parameters:
      c1 - the Collection to examine
      c2 - the entries to check
      Returns:
      a List of entries contained within c1, but not c2
      Throws:
      NullPointerException - if c1 or c2 are null
    • hasDelta

      public static <T> boolean hasDelta(Collection<T> c1, Collection<T> c2)
      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 entries
      c2 - the second Collection of entries
      Returns:
      TRUE if c1.size() != c2.size() or !c1.containsAll(c2), otherwise FALSE
    • join

      @SafeVarargs public static <T> Collection<T> join(Collection<T>... data)
      Joins multiple Collections into a single Collection without duplicates.
      Parameters:
      data - the Collections to join
      Returns:
      a Collection of unique results
    • createMap

      public static <K,V> Map<K,V> createMap(Collection<V> values, Function<V,K> f)
      Converts a Collection into a Map.
      Parameters:
      values - the values to use
      f - the function to call on each value to get the key value
      Returns:
      a Map of the values, indexed by their key
    • createMap

      public static <K,V> Map<K,V> createMap(Collection<K> keys, Collection<V> values)
      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 objects
      values - a Collection of value objects
      Returns:
      a Map of key/value pairs.
    • sort

      public static <T> List<T> sort(Collection<T> c, Comparator<T> cmp)
      Clones and sorts a Collection using a particular Comparator.
      Parameters:
      c - the Collection to sort
      cmp - the Comparator
      Returns:
      a sorted Collection
    • union

      public static <T> Collection<T> union(Collection<T> c1, Collection<T> c2)
      Examines two Collections and returns the items present in both.
      Parameters:
      c1 - the first Collection
      c2 - the second Collection
      Returns:
      a Collection of items present in both c1 and c2
    • addMapCollection

      public static <K,V> void addMapCollection(Map<K, Collection<V>> m, K key, V entry)
      Adds an entry to a Collection contained as a Map value.
      Parameters:
      m - the Map
      key - the key
      entry - 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 Map
      key - the key
      entry - the value
      cf - the Collection Supplier
    • nonNull

      @SafeVarargs public static <T> Collection<T> nonNull(T... values)
      Creates a Collection of non-null values.
      Parameters:
      values - the values to add
      Returns:
      a Collection of non-null values
    • nonNull

      @SafeVarargs public static <T> Collection<T> nonNull(Supplier<Collection<T>> cf, T... values)
      Creates a Collection of non-null values.
      Parameters:
      cf - the Collection factory function
      values - the values to add
      Returns:
      a Collection of non-null values