Package com.globalmentor.collections
Class Maps
- java.lang.Object
-
- com.globalmentor.collections.Maps
-
public class Maps extends java.lang.Object
Various utilities to be used with objects implementing theMap
interface.- Author:
- Garret Wilson
-
-
Constructor Summary
Constructors Constructor Description Maps()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K,V>
java.util.Map<K,V>addAll(java.util.Map<K,V> map, java.lang.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>
java.util.Map<N,V>addAll(java.util.Map<N,V> map, NameValuePair<N,V>[] nameValuePairs)
Adds values from an array of name-value pairs to a map.static <K,V>
java.util.Set<NameValuePair<K,V>>getKeyValues(java.util.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 java.util.Collection<NameValuePair<K,V>>>
CgetKeyValues(java.util.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 java.util.Collection<NameValuePair<K,V>>>
CgetKeyValuesCloned(java.util.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>
VputRemoveNull(java.util.Map<K,V> map, K key, V value)
Associates the specified value with the specified key in this map.static <K,V>
voidretainAll(java.util.Map<K,V> map, java.util.Set<K> set)
Retains only the entries in the map the keys of which have a corresponding entry in the given map.static <K,V>
java.util.Map<K,V>toImmutableMap(java.util.Map<K,V> map)
Creates a read-only copy of the given map.
-
-
-
Method Detail
-
addAll
public static <N,V> java.util.Map<N,V> addAll(java.util.Map<N,V> map, NameValuePair<N,V>[] nameValuePairs)
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
public static <K,V> java.util.Map<K,V> addAll(java.util.Map<K,V> map, java.lang.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. 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
public static <K,V> V putRemoveNull(java.util.Map<K,V> map, K key, V value)
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:
java.lang.UnsupportedOperationException
- Thrown if theput
operation is not supported by this map.java.lang.ClassCastException
- Thrown if the class of the specified key or value prevents it from being stored in this map.java.lang.IllegalArgumentException
- if some aspect of this key or value prevents it from being stored in this map.java.lang.NullPointerException
- this map does not permitnull
keys, and the specified key isnull
.
-
retainAll
public static <K,V> void retainAll(java.util.Map<K,V> map, java.util.Set<K> set)
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
public static <K,V> java.util.Set<NameValuePair<K,V>> getKeyValues(java.util.Map<K,V> map)
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 extends java.util.Collection<NameValuePair<K,V>>> C getKeyValues(java.util.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 extends java.util.Collection<NameValuePair<K,V>>> C getKeyValuesCloned(java.util.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
public static <K,V> java.util.Map<K,V> toImmutableMap(java.util.Map<K,V> map)
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:
java.lang.NullPointerException
- if the given map isnull
.
-
-