Class Maps

java.lang.Object
com.globalmentor.collections.Maps

public class Maps extends Object
Various utilities to be used with objects implementing the Map interface.
Author:
Garret Wilson
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <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>>>
    C
    getKeyValues(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>>>
    C
    getKeyValuesCloned(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
    retainAll(Map<K,V> map, 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> 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Maps

      public Maps()
  • Method Details

    • addAll

      public 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. 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> 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. 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(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 is null, 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, or null 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 the put 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 permit null keys, and the specified key is null.
    • retainAll

      public static <K, V> void retainAll(Map<K,V> map, 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> 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.
      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 Collection<NameValuePair<K, V>>> C getKeyValues(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 Collection<NameValuePair<K, V>>> C getKeyValuesCloned(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> Map<K,V> toImmutableMap(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:
      NullPointerException - if the given map is null.
    • toProperties

      public static Properties toProperties(@Nonnull Map<?,?> map)
      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.