Class CollectionUtils
java.lang.Object
com.kingsrook.qqq.backend.core.utils.CollectionUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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>
voidaddIfNotNull
(Collection<T> c, E element) add an element to a collection, but, only if the element isn't nullstatic <K> boolean
containsKeyWithNonNullValue
(Map<K, ?> map, K key) getPages
(Collection<T> values, int pageSize) split a large collection into lists of lists, with specified pageSizestatic String
getQuestionMarks
(Collection<?> c) build comma-delimited string of question marks from a collection (e.g., for an sql string)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> 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> nonNullCollection
(Collection<T> c) 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 emptystatic boolean
nullSafeHasContents
(Map<?, ?> c) true if c is NOT null and it's not emptystatic boolean
nullSafeIsEmpty
(Collection<?> c) true if c is null or it's emptystatic boolean
nullSafeIsEmpty
(Map<?, ?> c) true if c is null or it's emptystatic int
nullSafeSize
(Collection<?> c) 0 if c is empty, otherwise, its size.static int
nullSafeSize
(Map<?, ?> c) 0 if c is empty, otherwise, its size.static Map
<?, ?> 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.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.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 recordsstatic <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.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>>
TuseOrWrap
(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>>
TFor 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.
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
nullSafeIsEmpty
true if c is null or it's empty -
nullSafeIsEmpty
true if c is null or it's empty -
nullSafeHasContents
true if c is NOT null and it's not empty -
nullSafeHasContents
true if c is NOT null and it's not empty -
nullSafeSize
0 if c is empty, otherwise, its size. -
nullSafeSize
0 if c is empty, otherwise, its size. -
addAllToMap
-
listToMap
-
listToMap
public static <E,K, Map<K,V> 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, ListingHash<K,V> 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 mappedkeyFunction
- function to map an object from elements list into keys for the listing hashvalueFunction
- 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, Map<K1,V> Map<K2, listTo2LevelMapV>> (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
split a large collection into lists of lists, with specified pageSize -
getQuestionMarks
build comma-delimited string of question marks from a collection (e.g., for an sql string) -
safelyGetPage
-
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
-
nonNullCollection
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
-
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
-
mergeLists
-
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 useOrWrapT extends Map<K, V>> (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
-
containsKeyWithNonNullValue
-
addIfNotNull
add an element to a collection, but, only if the element isn't null
-