Package com.pnfsoftware.jeb.util.collect
Class Maps
java.lang.Object
com.pnfsoftware.jeb.util.collect.Maps
Utility methods for
maps
.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> List<V> Add a list bucket for the key in the provided multi-map.static <K,
V, C extends Collection<V>>
Collection<V>collectMulti
(Map<K, C> map) Collect the values of a multi-map.static <K,
V extends Comparable<V>>
LinkedHashMap<K,V> createSortedMapByValue
(Map<K, V> map, boolean ascending) Sort a map by its values.static <K,
V> V Get a value from a potentially null map.static <K,
V> V Get a value from a potentially null map.static <K,
V extends Comparable<V>>
List<Map.Entry<K,V>> getSortedEntriesByValue
(Map<K, V> map) Retrieve a list of sorted (key,value) pairs from the provided map, sorted by ascending values.static <K,
V extends Comparable<V>>
List<Map.Entry<K,V>> getSortedEntriesByValue
(Map<K, V> map, boolean descending) Retrieve a list of sorted (key,value) pairs from the provided map, sorted by values.static <K,
V extends Comparable<V>>
List<V>getSortedValues
(Map<K, V> map) Retrieve the values of a map, sorted by natural ascending.static <K,
V extends Comparable<V>>
List<V>getSortedValues
(Map<K, V> map, boolean ascending) Retrieve the values of a map, sorted by natural ascending or descending order.static <K,
V, C extends Collection<V>>
booleanAdd a key-value entry to the provided multi-map.static <K,
V> List<V> putMulti
(Map<K, List<V>> map, K key, Collection<V> values) Add a collection of key-value entries to the provided multi-map.static <K,
V> List<V> Add a key-value entry to the provided multi-map.static <V> String
putNoOverwrite
(Map<String, V> map, String preferredKey, V value) Insert a value to a string-key'ed map with guarantee that no existing key-value pair will be overwritten.static <K> boolean
removeAll
(Map<K, ?> map, Collection<? extends K> keys) static <K,
V> int removeForValue
(Map<K, V> map, V v, boolean useReferenceEquality) Remove entries that map to the provided value.static <K,
V, C extends Collection<V>>
booleanremoveMulti
(Map<K, C> map, K key, V value) Remove a key-value stored in a multi-map.static <K> boolean
retainAll
(Map<K, ?> map, Collection<? extends K> keys) static <K,
V> HashMap<K, V> toMap
(K key, V value) Build aHashMap
containing 1 element (key, value).static <K,
V> Map<K, V> Build a map containing 1 element (key, value).static <K,
V> HashMap<K, V> toMap
(K key, V value, K key2, V value2) Build aHashMap
containing 2 elements (key, value).static <K,
V> HashMap<K, V> toMap
(K key, V value, K key2, V value2, K key3, V value3) Build aHashMap
containing 3 elements (key, value).
-
Constructor Details
-
Maps
public Maps()
-
-
Method Details
-
get
Get a value from a potentially null map.- Parameters:
map
- optional mapkey
- key- Returns:
- the value, null if the map is null or contains a key pointing to a null value
-
get
Get a value from a potentially null map.- Parameters:
map
- optional mapkey
- keysafeValue
- value returned if the map is null or does not contain the provided key- Returns:
- the value - note: the returned value can be null if the key is present, but points to a null value
-
toMap
Build aHashMap
containing 1 element (key, value).- Parameters:
key
- keyvalue
- value associated to key- Returns:
-
toMap
Build aHashMap
containing 2 elements (key, value).- Parameters:
key
- keyvalue
- value associated to keykey2
- 2nd keyvalue2
- value associated to 2nd key- Returns:
-
toMap
Build aHashMap
containing 3 elements (key, value).- Parameters:
key
- keyvalue
- value associated to keykey2
- 2nd keyvalue2
- value associated to 2nd keykey3
- 3rd keyvalue3
- value associated to 3rd key- Returns:
-
toMap
Build a map containing 1 element (key, value).- Parameters:
key
- keyvalue
- value associated to keyc
- optional requested map type; use null to obtain aHashMap
- Returns:
-
getSortedValues
Retrieve the values of a map, sorted by natural ascending or descending order. The values must beComparable
objects.- Parameters:
map
- a map; it must not contain null valuesascending
- true to sort by ascending order, false to sort by descending order- Returns:
- a list of sorted values; the list can be modified without impacting the map's structure
-
getSortedValues
Retrieve the values of a map, sorted by natural ascending. The values must beComparable
objects.- Parameters:
map
- a map; it must not contain null values- Returns:
- a list of sorted values; the list can be modified without impacting the map's structure
-
createSortedMapByValue
public static <K,V extends Comparable<V>> LinkedHashMap<K,V> createSortedMapByValue(Map<K, V> map, boolean ascending) Sort a map by its values. The values must beComparable
objects.- Parameters:
map
- a map; it must not contain null valuesascending
- true to sort by ascending order, false to sort by descending order- Returns:
- a map ordered by its values; this map can be modified without impacting the original map's structure
-
putNoOverwrite
Insert a value to a string-key'ed map with guarantee that no existing key-value pair will be overwritten.- Parameters:
map
-preferredKey
- the preferred key for insertionvalue
-- Returns:
- the actual key that was generated for insertion (may be different than the Preferred if using the preferred would lead to an existing mapping to be overwritten)
-
removeAll
- Type Parameters:
K
-- Parameters:
map
-keys
-- Returns:
-
retainAll
- Type Parameters:
K
-- Parameters:
map
-keys
-- Returns:
-
addMulti
Add a list bucket for the key in the provided multi-map. If no list of values exists for the key, a newArrayList
is created with an initial capacity of one.- Type Parameters:
K
-V
-- Parameters:
map
-key
-- Returns:
-
putMulti
Add a key-value entry to the provided multi-map. If no list of values exists for the key, a newArrayList
is created with an initial capacity of one (to accommodate the addition of the value).Equivalent to
putMulti(Map, Object, Object, Supplier)
with supplier beingArrayList::new
.- Type Parameters:
K
-V
-- Parameters:
map
-key
-value
-- Returns:
- the list for the provided key
-
putMulti
Add a collection of key-value entries to the provided multi-map. If no list of values exists for the key, a newArrayList
is created.- Type Parameters:
K
-V
-- Parameters:
map
-key
-values
-- Returns:
-
putMulti
public static <K,V, boolean putMultiC extends Collection<V>> (Map<K, C> map, K key, V value, Supplier<C> supplier) Add a key-value entry to the provided multi-map. If no list of values exists for the key, a new list is created using the provided listFactory parameter.- Type Parameters:
K
- key typeV
- value typeC
- collection-of-value type- Parameters:
map
- the mapkey
- the keyvalue
- value to be added to the collection mapped for keysupplier
- collection factory if no collection exists and is mapped for the provided key- Returns:
- true if the collection to which the value was added changed (i.e., same semantic as
Collection.add(Object)
)
-
collectMulti
Collect the values of a multi-map.- Type Parameters:
K
- key typeV
- value type stored in the collection valuesC
- collection type, mapped by the keys- Parameters:
map
- a map of key-collections- Returns:
- the collection of values stored in the multi-map
-
removeMulti
Remove a key-value stored in a multi-map.- Type Parameters:
K
- key typeV
- value typeC
- collection-of-value type- Parameters:
map
- a map of key-collectionskey
-value
-- Returns:
- true if a key-value was removed, false otherwise
-
removeForValue
Remove entries that map to the provided value.- Type Parameters:
K
-V
-- Parameters:
map
- mapv
- a non-null valueuseReferenceEquality
- if true, values are compared using ==; else, they are compared using equals()- Returns:
- the number of entries removed
-
getSortedEntriesByValue
public static <K,V extends Comparable<V>> List<Map.Entry<K,V>> getSortedEntriesByValue(Map<K, V> map) Retrieve a list of sorted (key,value) pairs from the provided map, sorted by ascending values.- Type Parameters:
K
- key typeV
- value type, which must implementComparable
- Parameters:
map
- the input map (preferably not containing null values)
-
getSortedEntriesByValue
public static <K,V extends Comparable<V>> List<Map.Entry<K,V>> getSortedEntriesByValue(Map<K, V> map, boolean descending) Retrieve a list of sorted (key,value) pairs from the provided map, sorted by values.- Type Parameters:
K
- key typeV
- value type, which must implementComparable
- Parameters:
map
- the input map (should not contain null values)descending
- if true, the list is sorted by descending values instead of ascending values
-