|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.Maps
public final class Maps
Static utility methods pertaining to Map instances. Also see this
class's counterparts Lists and Sets.
| Method Summary | ||
|---|---|---|
static boolean |
containsEntry(java.util.Map<?,?> map,
java.lang.Object key,
java.lang.Object value)
Returns true if map contains an entry mapping key
to value. |
|
static java.util.Map<java.lang.String,java.lang.String> |
fromProperties(java.util.Properties properties)
Creates a Map<String, String> from a Properties instance. |
|
static
|
immutableEntry(K key,
V value)
Returns an immutable map entry with the specified key and value. |
|
static
|
newClassToInstanceMap()
Returns a new ClassToInstanceMap instance backed by a HashMap using the default initial capacity and load factor. |
|
static
|
newClassToInstanceMap(java.util.Map<java.lang.Class<? extends B>,B> backingMap)
Returns a new ClassToInstanceMap instance backed by a given empty
backingMap. |
|
static
|
newConcurrentHashMap()
Creates a ConcurrentHashMap instance. |
|
static
|
newEnumBiMap(java.lang.Class<K> keyType,
java.lang.Class<V> valueType)
Returns a new empty EnumBiMap using the specified key and value
types. |
|
static
|
newEnumHashBiMap(java.lang.Class<K> keyType)
Returns a new empty EnumHashBiMap using the specified key type. |
|
static
|
newEnumMap(java.lang.Class<K> type)
Creates an EnumMap instance. |
|
static
|
newHashBiMap()
Returns a new empty HashBiMap with the default initial capacity
(16). |
|
static
|
newHashMap()
Creates a HashMap instance. |
|
static
|
newHashMap(java.util.Map<? extends K,? extends V> map)
Creates a HashMap instance with the same mappings as the specified
map. |
|
static
|
newHashMapWithExpectedSize(int expectedSize)
Creates a HashMap instance with enough capacity to hold the
specified number of elements without rehashing. |
|
static
|
newIdentityHashMap()
Creates an IdentityHashMap instance. |
|
static
|
newLinkedHashMap()
Creates an insertion-ordered LinkedHashMap instance. |
|
static
|
newLinkedHashMap(java.util.Map<? extends K,? extends V> map)
Creates an insertion-ordered LinkedHashMap instance with the same
mappings as the specified map. |
|
static
|
newTreeMap()
Creates a TreeMap instance using the natural ordering of its
elements. |
|
static
|
newTreeMap(java.util.Comparator<C> comparator)
Creates a TreeMap instance using the given comparator. |
|
static
|
synchronizedBiMap(BiMap<K,V> bimap)
Returns a synchronized (thread-safe) bimap backed by the specified bimap. |
|
static
|
uniqueIndex(java.lang.Iterable<V> values,
Function<? super V,K> keyFunction)
Returns an immutable map for which the Map.values() are the given
elements in the given order, and each key is the product of invoking a
supplied function on its corresponding value. |
|
static
|
unmodifiableBiMap(BiMap<K,V> bimap)
Returns an unmodifiable view of the specified bimap. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <K,V> java.util.HashMap<K,V> newHashMap()
HashMap instance.
Note: if K is an enum type, use newEnumMap(java.lang.Class instead.
Note: if you don't actually need the resulting map to be mutable,
use Collections.emptyMap() instead.
HashMappublic static <K,V> java.util.HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)
HashMap instance with enough capacity to hold the
specified number of elements without rehashing.
expectedSize - the expected size
HashMap, initially empty, with enough
capacity to hold expectedSize elements without rehashing
java.lang.IllegalArgumentException - if expectedSize is negativepublic static <K,V> java.util.HashMap<K,V> newHashMap(java.util.Map<? extends K,? extends V> map)
HashMap instance with the same mappings as the specified
map.
Note: if K is an Enum type, use newEnumMap(java.lang.Class instead.
map - the mappings to be placed in the new map
HashMap initialized with the mappings from
mappublic static <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMap()
LinkedHashMap instance.
LinkedHashMappublic static <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMap(java.util.Map<? extends K,? extends V> map)
LinkedHashMap instance with the same
mappings as the specified map.
map - the mappings to be placed in the new map
LinkedHashMap initialized with the
mappings from mappublic static <K,V> java.util.concurrent.ConcurrentHashMap<K,V> newConcurrentHashMap()
ConcurrentHashMap instance.
ConcurrentHashMappublic static <K extends java.lang.Comparable,V> java.util.TreeMap<K,V> newTreeMap()
TreeMap instance using the natural ordering of its
elements.
TreeMap
public static <C,K extends C,V> java.util.TreeMap<K,V> newTreeMap(@Nullable
java.util.Comparator<C> comparator)
TreeMap instance using the given comparator.
comparator - the comparator to sort the keys with
TreeMappublic static <K extends java.lang.Enum<K>,V> java.util.EnumMap<K,V> newEnumMap(java.lang.Class<K> type)
EnumMap instance.
type - the key type for this map
EnumMappublic static <K,V> java.util.IdentityHashMap<K,V> newIdentityHashMap()
IdentityHashMap instance.
IdentityHashMap
public static boolean containsEntry(java.util.Map<?,?> map,
@Nullable
java.lang.Object key,
@Nullable
java.lang.Object value)
true if map contains an entry mapping key
to value. If you are not concerned with null-safety you can simply
use map.get(key).equals(value).
public static <K,V> BiMap<K,V> synchronizedBiMap(BiMap<K,V> bimap)
It is imperative that the user manually synchronize on the returned map when accessing any of its collection views:
Bimap<K,V> m = Maps.synchronizedBiMap(
new HashBiMap<K,V>());
...
Set<K> s = m.keySet(); // Needn't be in synchronized block
...
synchronized (m) { // Synchronizing on m, not s!
Iterator<K> i = s.iterator(); // Must be in synchronized block
while (i.hasNext()) {
foo(i.next());
}
}
Failure to follow this advice may result in non-deterministic behavior.
bimap - the bimap to be wrapped in a synchronized view
public static <K,V> ImmutableMap<K,V> uniqueIndex(java.lang.Iterable<V> values,
Function<? super V,K> keyFunction)
Map.values() are the given
elements in the given order, and each key is the product of invoking a
supplied function on its corresponding value.
values - the values to use when constructing the MapkeyFunction - the function used to produce the key for each value
keyFunction on each value in the input collection to that value
java.lang.IllegalArgumentException - if keyFunction produces the same
key for more than one value in the input collection
java.lang.NullPointerException - if any elements of values is null, or
if keyFunction produces null for any valuepublic static java.util.Map<java.lang.String,java.lang.String> fromProperties(java.util.Properties properties)
Map<String, String> from a Properties instance.
Properties normally derive from Map<Object, Object>, but they
typically contain strings, which is awkward. This method lets you get a
plain-old-Map out of a Properties. The returned map won't
include any null keys or values. The returned map is modifiable and
serializable.
properties - a Properties object to be converted
properties
public static <K,V> java.util.Map.Entry<K,V> immutableEntry(@Nullable
K key,
@Nullable
V value)
Map.Entry.setValue(V) operation throws an UnsupportedOperationException.
The returned entry is serializable.
key - the key to be associated with the returned entryvalue - the value to be associated with the returned entrypublic static <K,V> HashBiMap<K,V> newHashBiMap()
HashBiMap with the default initial capacity
(16).
public static <K extends java.lang.Enum<K>,V> EnumHashBiMap<K,V> newEnumHashBiMap(java.lang.Class<K> keyType)
EnumHashBiMap using the specified key type.
keyType - the key type
public static <K extends java.lang.Enum<K>,V extends java.lang.Enum<V>> EnumBiMap<K,V> newEnumBiMap(java.lang.Class<K> keyType,
java.lang.Class<V> valueType)
EnumBiMap using the specified key and value
types.
keyType - the key typevalueType - the value typepublic static <K,V> BiMap<K,V> unmodifiableBiMap(BiMap<K,V> bimap)
UnsupportedOperationException.
The returned bimap will be serializable if the specified bimap is serializable.
bimap - the bimap for which an unmodifiable view is to be returned
public static <B> ClassToInstanceMap<B> newClassToInstanceMap()
ClassToInstanceMap instance backed by a HashMap using the default initial capacity and load factor.
public static <B> ClassToInstanceMap<B> newClassToInstanceMap(java.util.Map<java.lang.Class<? extends B>,B> backingMap)
ClassToInstanceMap instance backed by a given empty
backingMap. The caller surrenders control of the backing map, and
thus should not allow any direct references to it to remain accessible.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||