public class

Maps

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

Class Overview

Utility methods for map.

Summary

Public Constructors
Maps()
Public Methods
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<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 <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, 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 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<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 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 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 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