Class CollectionUtils

java.lang.Object
com.kingsrook.qqq.backend.core.utils.CollectionUtils

public class CollectionUtils extends Object
Utility class for working with Collections.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K, V> void
    addAllToMap(Map<K,V> addingTo, Map<K,V> addingFrom)
    add all values from one map to another.
    static <T, E extends T>
    void
    addIfNotNull(Collection<T> c, E element)
    add an element to a collection, but, only if the element isn't null
    static <K> boolean
    containsKeyWithNonNullValue(Map<K,?> map, K key)
     
    static <T> List<List<T>>
    getPages(Collection<T> values, int pageSize)
    split a large collection into lists of lists, with specified pageSize
    static String
    build comma-delimited string of question marks from a collection (e.g., for an sql string)
    static <K1, K2, V>
    Map<K1,Map<K2,V>>
    listTo2LevelMap(List<V> values, Function<V,? extends K1> keyFunction1, Function<V,? extends K2> keyFunction2)
    Convert a list to a 2-level Map (ie., Map of Map of Key,Value), where 2 lambdas are provided for extract the two levels of keys from the objects in the list, and the values in the map are the values in the list.
    static <E, K, V> ListingHash<K,V>
    listToListingHash(List<E> elements, Function<E,? extends K> keyFunction, Function<E,? extends V> valueFunction)
    Take a list of objects, and build a listing hash, using 2 lambdas to control how keys in the listing hash are created (from the objects), and how values are created.
    static <K, V> ListingHash<K,V>
    listToListingHash(List<V> values, Function<V,? extends K> keyFunction)
    Build a listingHash from a list, supplying a function that extracts keys from the objects in the list (the objects in the list become the values in the map).
    static <E, K, V> Map<K,V>
    listToMap(List<E> elements, Function<E,? extends K> keyFunction, Function<E,? extends V> valueFunction)
    Build a map from a list, supplying a function that extracts keys from the objects in the list, and another function that extracts the values from the objects in the list.
    static <K, V> Map<K,V>
    listToMap(List<V> values, Function<V,? extends K> keyFunction)
    Build a map from a list, supplying a function that extracts keys from the objects in the list (the objects in the list become the values in the map).
    static <T> List<T>
    mergeLists(List<T>... lists)
     
    static <T> T[]
    nonNullArray(T[] array)
    Returns the input array, unless it was null - in which case a new (empty) array is returned.
    static <T> Collection<T>
    Returns the input collection, unless it was null - in which case a new array list is returned.
    static <T> List<T>
    nonNullList(List<T> list)
    Returns the input list, unless it was null - in which case a new array list is returned.
    static <K, V> Map<K,V>
    nonNullMap(Map<K,V> map)
    Returns the input map, unless it was null - in which case a new HashMap is returned.
    static boolean
    true if c is NOT null and it's not empty
    static boolean
    true if c is NOT null and it's not empty
    static boolean
    true if c is null or it's empty
    static boolean
    true if c is null or it's empty
    static int
    0 if c is empty, otherwise, its size.
    static int
    nullSafeSize(Map<?,?> c)
    0 if c is empty, otherwise, its size.
    static Map<?,?>
     
    recordsToMap(Collection<QRecord> records, String keyFieldName)
    Convert a collection of QRecords to a map, from one field's values out of those records, to the records themselves.
    static <T extends Serializable>
    Map<T,QRecord>
    recordsToMap(Collection<QRecord> records, String keyFieldName, Class<T> type)
    Convert a collection of QRecords to a map, from one field's values out of those records, to the records themselves.
    recordsToMap(Collection<QRecord> records, String keyFieldName, String valueFieldName)
    Convert a collection of QRecords to a map, from one field's values out of those records, to another field's value from those records
    static <T> List<T>
    safelyGetPage(List<T> list, Integer skip, Integer limit)
    Get a sub-list, safely - i.e., w/o worrying if your indexes are too big.
    static <K1, K2, V>
    Map<K2,Map<K1,V>>
    swapMultiLevelMapKeys(Map<K1,Map<K2,V>> input)
    Take a multi-level map, e.g., Map{String, Map{Integer, BigDecimal}} and invert it - e.g., to Map{Integer, Map{String, BigDecimal}}
    static <E, T extends Collection<E>>
    T
    useOrWrap(Collection<E> collection, com.google.gson.reflect.TypeToken<T> typeToken)
    For cases where you have a Collection (of an unknown type), and you know you want/need it in a specific concrete type (say, ArrayList), but you don't want to just blindly copy it (e.g., as that may be expensive), call this method.
    static <K, V, T extends Map<K, V>>
    T
    useOrWrap(Map<K,V> collection, com.google.gson.reflect.TypeToken<T> typeToken)
    For cases where you have a Collection (of an unknown type), and you know you want/need it in a specific concrete type (say, ArrayList), but you don't want to just blindly copy it (e.g., as that may be expensive), call this method.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CollectionUtils

      public CollectionUtils()
  • Method Details

    • nullSafeIsEmpty

      public static boolean nullSafeIsEmpty(Collection<?> c)
      true if c is null or it's empty
    • nullSafeIsEmpty

      public static boolean nullSafeIsEmpty(Map<?,?> c)
      true if c is null or it's empty
    • nullSafeHasContents

      public static boolean nullSafeHasContents(Collection<?> c)
      true if c is NOT null and it's not empty
    • nullSafeHasContents

      public static boolean nullSafeHasContents(Map<?,?> c)
      true if c is NOT null and it's not empty
    • nullSafeSize

      public static int nullSafeSize(Collection<?> c)
      0 if c is empty, otherwise, its size.
    • nullSafeSize

      public static int nullSafeSize(Map<?,?> c)
      0 if c is empty, otherwise, its size.
    • addAllToMap

      public static <K, V> void addAllToMap(Map<K,V> addingTo, Map<K,V> addingFrom)
      add all values from one map to another.
    • listToMap

      public static <K, V> Map<K,V> listToMap(List<V> values, Function<V,? extends K> keyFunction)
      Build a map from a list, supplying a function that extracts keys from the objects in the list (the objects in the list become the values in the map).
    • listToMap

      public static <E, K, V> Map<K,V> listToMap(List<E> elements, Function<E,? extends K> keyFunction, Function<E,? extends V> valueFunction)
      Build a map from a list, supplying a function that extracts keys from the objects in the list, and another function that extracts the values from the objects in the list.
    • listToListingHash

      public static <K, V> ListingHash<K,V> listToListingHash(List<V> values, Function<V,? extends K> keyFunction)
      Build a listingHash from a list, supplying a function that extracts keys from the objects in the list (the objects in the list become the values in the map).
    • listToListingHash

      public static <E, K, V> ListingHash<K,V> listToListingHash(List<E> elements, Function<E,? extends K> keyFunction, Function<E,? extends V> valueFunction)

      Take a list of objects, and build a listing hash, using 2 lambdas to control how keys in the listing hash are created (from the objects), and how values are created.

      For example, given a list of Work records, if we want a Listing hash with workStatusId as keys, and workId a values, we would call:

      listToListingHash(workList, Work::getWorkStatusId, Work::getId)
      Parameters:
      elements - list of objects to be mapped
      keyFunction - function to map an object from elements list into keys for the listing hash
      valueFunction - function to map an object from elements list into values for the listing hash
      Returns:
      ListingHash that might look like: 1 -> [ 73, 75, 68] 2 -> [ 74 ] 4 -> [ 76, 77, 79, 80, 81] end
    • listTo2LevelMap

      public static <K1, K2, V> Map<K1,Map<K2,V>> listTo2LevelMap(List<V> values, Function<V,? extends K1> keyFunction1, Function<V,? extends K2> keyFunction2)
      Convert a list to a 2-level Map (ie., Map of Map of Key,Value), where 2 lambdas are provided for extract the two levels of keys from the objects in the list, and the values in the map are the values in the list.
    • getPages

      public static <T> List<List<T>> getPages(Collection<T> values, int pageSize)
      split a large collection into lists of lists, with specified pageSize
    • getQuestionMarks

      public static String getQuestionMarks(Collection<?> c)
      build comma-delimited string of question marks from a collection (e.g., for an sql string)
    • safelyGetPage

      public static <T> List<T> safelyGetPage(List<T> list, Integer skip, Integer limit)
      Get a sub-list, safely - i.e., w/o worrying if your indexes are too big.
    • nonNullArray

      public static <T> T[] nonNullArray(T[] array)
      Returns the input array, unless it was null - in which case a new (empty) array is returned. Meant to help avoid null checks on foreach loops.
    • nonNullList

      public static <T> List<T> nonNullList(List<T> list)
      Returns the input list, unless it was null - in which case a new array list is returned. Meant to help avoid null checks on foreach loops.
    • nonNullCollection

      public static <T> Collection<T> nonNullCollection(Collection<T> c)
      Returns the input collection, unless it was null - in which case a new array list is returned. Meant to help avoid null checks on foreach loops.
    • nonNullMap

      public static <K, V> Map<K,V> nonNullMap(Map<K,V> map)
      Returns the input map, unless it was null - in which case a new HashMap is returned. Meant to help avoid null checks on foreach loops.
    • recordsToMap

      public static Map<Serializable,Serializable> recordsToMap(Collection<QRecord> records, String keyFieldName, String valueFieldName)
      Convert a collection of QRecords to a map, from one field's values out of those records, to another field's value from those records
    • recordsToMap

      public static Map<Serializable,QRecord> recordsToMap(Collection<QRecord> records, String keyFieldName)
      Convert a collection of QRecords to a map, from one field's values out of those records, to the records themselves.
    • recordsToMap

      public static <T extends Serializable> Map<T,QRecord> recordsToMap(Collection<QRecord> records, String keyFieldName, Class<T> type)
      Convert a collection of QRecords to a map, from one field's values out of those records, to the records themselves.
    • objectToMap

      public static Map<?,?> objectToMap(Object o)
    • mergeLists

      @SafeVarargs public static <T> List<T> mergeLists(List<T>... lists)
    • useOrWrap

      public static <E, T extends Collection<E>> T useOrWrap(Collection<E> collection, com.google.gson.reflect.TypeToken<T> typeToken)
      For cases where you have a Collection (of an unknown type), and you know you want/need it in a specific concrete type (say, ArrayList), but you don't want to just blindly copy it (e.g., as that may be expensive), call this method. ArrayList[String] myStrings = CollectionUtils.useOrWrap(yourStrings, new TypeTokeninvalid input: '<'>() {}); CollectionUtils.useOrWrap(yourStrings, TypeToken.get(ArrayList.class)); Note that you may always just pass `new TypeToken() {}` as the 2nd arg - then the compiler will infer the type (T) based on the variable you're assigning into.
    • useOrWrap

      public static <K, V, T extends Map<K, V>> T useOrWrap(Map<K,V> collection, com.google.gson.reflect.TypeToken<T> typeToken)
      For cases where you have a Collection (of an unknown type), and you know you want/need it in a specific concrete type (say, ArrayList), but you don't want to just blindly copy it (e.g., as that may be expensive), call this method. HashMap[String,Integer] myMap = CollectionUtils.useOrWrap(yourMap, new TypeTokeninvalid input: '<'>() {}); CollectionUtils.useOrWrap(yourMap, TypeToken.get(HashMap.class)); Note that you may always just pass `new TypeToken() {}` as the 2nd arg - then the compiler will infer the type (T) based on the variable you're assigning into.
    • swapMultiLevelMapKeys

      public static <K1, K2, V> Map<K2,Map<K1,V>> swapMultiLevelMapKeys(Map<K1,Map<K2,V>> input)
      Take a multi-level map, e.g., Map{String, Map{Integer, BigDecimal}} and invert it - e.g., to Map{Integer, Map{String, BigDecimal}}
    • containsKeyWithNonNullValue

      public static <K> boolean containsKeyWithNonNullValue(Map<K,?> map, K key)
    • addIfNotNull

      public static <T, E extends T> void addIfNotNull(Collection<T> c, E element)
      add an element to a collection, but, only if the element isn't null