Package com.globalmentor.collections
Class Maps
java.lang.Object
com.globalmentor.collections.Maps
Various utilities to be used with objects implementing the
Map
interface.- Author:
- Garret Wilson
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Map<K, V> addAll
(Map<K, V> map, Iterable<NameValuePair<K, V>> iterable) Adds all given values from name-value pairs to a map, keyed to the names of those name-value pairs.static <N,
V> Map<N, V> addAll
(Map<N, V> map, NameValuePair<N, V>[] nameValuePairs) Adds values from an array of name-value pairs to a map.static <K,
V> Set<NameValuePair<K, V>> getKeyValues
(Map<K, V> map) Retrieves all map entries from the given map and returns them as a set of name value pairs.static <K,
V, C extends Collection<NameValuePair<K, V>>>
CgetKeyValues
(Map<K, V> map, C collection) Retrieves all map entries from the given map and returns them as name value pairs in the given collection.static <K,
V extends CloneSupported, C extends Collection<NameValuePair<K, V>>>
CgetKeyValuesCloned
(Map<K, V> map, C collection) Retrieves all map entries from the given map and returns them as name value pairs in the given collection; the values are cloned.static <K,
V> V putRemoveNull
(Map<K, V> map, K key, V value) Associates the specified value with the specified key in this map.static <K,
V> void Retains only the entries in the map the keys of which have a corresponding entry in the given map.static <K,
V> Map<K, V> toImmutableMap
(Map<K, V> map) Creates a read-only copy of the given map.static Properties
toProperties
(Map<?, ?> map) Converts the given map to a properties object.
-
Constructor Details
-
Maps
public Maps()
-
-
Method Details
-
addAll
Adds values from an array of name-value pairs to a map. If more than one pair with the same name is given, the last one will override the others.- Type Parameters:
N
- The type of the name used as key of the map.V
- The type of the value of the map.- Parameters:
map
- The map to receive the name-value pair names and values.nameValuePairs
- An array of name-value pairs.- Returns:
- The given map.
-
addAll
Adds all given values from name-value pairs to a map, keyed to the names of those name-value pairs. If more than one pair with the same name is given, the last one will override the others.- Type Parameters:
K
- The name-value pair name type.V
- The name-value pair value type.- Parameters:
map
- The map which will receive the name-value mappings.iterable
- The source of name-value pairs.- Returns:
- The given map.
-
putRemoveNull
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced by the specified value. If value isnull
, the value with the specified key will be removed from the map.- Type Parameters:
K
- The type of key that is used by the map.V
- The type of value that is used by the map.- Parameters:
map
- The map into which the value should be put.key
- The key with which the specified value is to be associated.value
- The value to be associated with the specified key, ornull
if the value associated with the key should be removed.- Returns:
- The previous value associated with specified key, or
null
if there was no mapping for key. - Throws:
UnsupportedOperationException
- Thrown if theput
operation is not supported by this map.ClassCastException
- Thrown if the class of the specified key or value prevents it from being stored in this map.IllegalArgumentException
- if some aspect of this key or value prevents it from being stored in this map.NullPointerException
- this map does not permitnull
keys, and the specified key isnull
.
-
retainAll
Retains only the entries in the map the keys of which have a corresponding entry in the given map.- Type Parameters:
K
- The type of keys used in the map.V
- The type of values stored in the map.- Parameters:
map
- The map from which to remove values.set
- The set to indicate which key entries to retain.
-
getKeyValues
Retrieves all map entries from the given map and returns them as a set of name value pairs.- Type Parameters:
K
- The type of key.V
- The type of value.- Parameters:
map
- The map from which to the retrieve the key values.- Returns:
- A set of the key value pairs added.
-
getKeyValues
public static <K,V, C getKeyValuesC extends Collection<NameValuePair<K, V>>> (Map<K, V> map, C collection) Retrieves all map entries from the given map and returns them as name value pairs in the given collection.- Type Parameters:
K
- The type of key.V
- The type of value.C
- The type of collection.- Parameters:
map
- The map from which to the retrieve the key values.collection
- The collection to which the key value pairs should be added.- Returns:
- The given collection, with the key value pairs added.
-
getKeyValuesCloned
public static <K,V extends CloneSupported, C getKeyValuesClonedC extends Collection<NameValuePair<K, V>>> (Map<K, V> map, C collection) Retrieves all map entries from the given map and returns them as name value pairs in the given collection; the values are cloned.- Type Parameters:
K
- The type of key.V
- The type of value.C
- The type of collection.- Parameters:
map
- The map from which to the retrieve the key values.collection
- The collection to which the key value pairs should be added.- Returns:
- The given collection, with the key value pairs added.
-
toImmutableMap
Creates a read-only copy of the given map. If the map is already read-only, the map itself is returned.- Type Parameters:
K
- The type of key contained in the map.V
- The type of value contained in the map.- Parameters:
map
- The map which should be returned in read-only form.- Returns:
- The immutable version of the map.
- Throws:
NullPointerException
- if the given map isnull
.
-
toProperties
Converts the given map to a properties object. If the map is already a properties object, it is returned. Otherwise, a new properties object is created and populated with the entries of the given map.- Parameters:
map
- The map to convert to a properties object.- Returns:
- A properties object, potentially the same instance, containing entries from the given map.
-