public class

Maps

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.collect.Maps

Class Overview

Utility methods for Map maps.

Summary

Public Constructors
Maps()
Public Methods
static <K, V> List<V> addMulti(Map<K, List<V>> map, K key)
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(Map<K, V> map, K key)
Get a value from a potentially null map.
static <K, V> V get(Map<K, V> map, K key, V safeValue)
Get a value from a potentially null map.
static <K, V extends Comparable<V>> List<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<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<V> getSortedValues(Map<K, V> map, boolean ascending)
Retrieve the values of a map, sorted by natural ascending or descending order.
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, C extends Collection<V>> boolean putMulti(Map<K, C> map, K key, V value, Supplier<C> supplier)
Add a key-value entry to the provided multi-map.
static <K, V> List<V> putMulti(Map<K, List<V>> map, K key, V value)
Add 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 <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>> boolean removeMulti(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 a HashMap containing 1 element (key, value).
static <K, V> Map<K, V> toMap(K key, V value, Class<? extends Map> c)
Build a map containing 1 element (key, value).
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Maps ()

Public Methods

public static List<V> addMulti (Map<K, List<V>> map, K key)

Add a list bucket for the key in the provided multi-map. If no list of values exists for the key, a new ArrayList is created with an initial capacity of one.

public static Collection<V> collectMulti (Map<K, C> map)

Collect the values of a multi-map.

Parameters
map a map of key-collections
Returns
  • the collection of values stored in the multi-map

public static LinkedHashMap<K, V> createSortedMapByValue (Map<K, V> map, boolean ascending)

Sort a map by its values. The values must be Comparable objects.

Parameters
map a map; it must not contain null values
ascending 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

public static V get (Map<K, V> map, K key)

Get a value from a potentially null map.

Parameters
map optional map
key key
Returns
  • the value, null if the map is null or contains a key pointing to a null value

public static V get (Map<K, V> map, K key, V safeValue)

Get a value from a potentially null map.

Parameters
map optional map
key key
safeValue 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

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

Parameters
map the input map (should not contain null values)
descending if true, the list is sorted by descending values instead of ascending values

public static List<Entry<K, V>> getSortedEntriesByValue (Map<K, V> map)

Retrieve a list of sorted (key,value) pairs from the provided map, sorted by ascending values.

Parameters
map the input map (preferably not containing null values)

public static List<V> getSortedValues (Map<K, V> map, boolean ascending)

Retrieve the values of a map, sorted by natural ascending or descending order. The values must be Comparable objects.

Parameters
map a map; it must not contain null values
ascending 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

public static List<V> getSortedValues (Map<K, V> map)

Retrieve the values of a map, sorted by natural ascending. The values must be Comparable 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

public static boolean putMulti (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.

Parameters
map the map
key the key
value value to be added to the collection mapped for key
supplier 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 add(Object))

public static List<V> putMulti (Map<K, List<V>> map, K key, V value)

Add a key-value entry to the provided multi-map. If no list of values exists for the key, a new ArrayList is created with an initial capacity of one (to accommodate the addition of the value).

Equivalent to putMulti(Map, Object, Object, Supplier) with supplier being ArrayList::new.

Returns
  • the list for the provided key

public static 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. If no list of values exists for the key, a new ArrayList is created.

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

Parameters
preferredKey the preferred key for insertion
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)

public static boolean removeAll (Map<K, ?> map, Collection<? extends K> keys)

public static int removeForValue (Map<K, V> map, V v, boolean useReferenceEquality)

Remove entries that map to the provided value.

Parameters
map map
v a non-null value
useReferenceEquality if true, values are compared using ==; else, they are compared using equals()
Returns
  • the number of entries removed

public static boolean removeMulti (Map<K, C> map, K key, V value)

Remove a key-value stored in a multi-map.

Parameters
map a map of key-collections
Returns
  • true if a key-value was removed, false otherwise

public static boolean retainAll (Map<K, ?> map, Collection<? extends K> keys)

public static HashMap<K, V> toMap (K key, V value)

Build a HashMap containing 1 element (key, value).

public static Map<K, V> toMap (K key, V value, Class<? extends Map> c)

Build a map containing 1 element (key, value).

Parameters
c optional requested map type; use null to obtain a HashMap