Top Description Inners Constructors Methods
com.google.common.collect

public abstract Class ForwardingMap<K extends @Nullable Object, V extends @Nullable Object>

extends ForwardingObject
implements Map<K extends @Nullable Object, V extends @Nullable Object>
Class Inheritance
All Implemented Interfaces
java.util.Map
Known Direct Subclasses
com.google.common.collect.ForwardingSortedMap, com.google.common.collect.ImmutableClassToInstanceMap, com.google.common.collect.Maps.UnmodifiableBiMap, com.google.common.collect.Maps.DescendingMap, com.google.common.collect.MutableClassToInstanceMap, com.google.common.reflect.ImmutableTypeToInstanceMap, com.google.common.reflect.MutableTypeToInstanceMap, com.google.common.collect.AbstractBiMap, com.google.common.collect.ForwardingConcurrentMap
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Imports
com.google.common.annotations.GwtCompatible, com.google.common.base.Objects, com.google.errorprone.annotations.CanIgnoreReturnValue, java.util.Collection, .Iterator, .Map, .Set, javax.annotation.CheckForNull, org.checkerframework.checker.nullness.qual.Nullable

A map which forwards all its method calls to another map. Subclasses should override one or more methods to modify the behavior of the backing map as desired per the decorator pattern.

Warning

The methods of ForwardingMap forward indiscriminately to the methods of the delegate. For example, overriding put alone will not change the behavior of putAll, which can lead to unexpected behavior. In this case, you should override putAll as well, either providing your own implementation, or delegating to the provided standardPutAll method.

default method warning: This class does not forward calls to default methods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on the ForwardingMap.

Each of the standard methods, where appropriate, use Objects#equal to test equality for both keys and values. This may not be the desired behavior for map implementations that use non-standard notions of key equality, such as a SortedMap whose comparator is not consistent with equals.

The standard methods and the collection views they return are not guaranteed to be thread-safe, even when all of the methods that they depend on are thread-safe.

Authors
Kevin Bourrillion, Jared Levy, Louis Wasserman
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected abstract class
protected class
protected class

Constructor Summary

AccessConstructor and Description
protected
ForwardingMap()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Implements java.util.Map.clear.

Removes all of the mappings from this map (optional operation).
public boolean
containsKey(Object
key whose presence in this map is to be tested
key
)

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.
public boolean
containsValue(Object
value whose presence in this map is to be tested
value
)

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.
protected abstract Map<K, V>
delegate()

Implements abstract com.google.common.collect.ForwardingObject.delegate.

Returns the backing delegate instance that methods are forwarded to.
public Set<Map.Entry<K, V>>
entrySet()

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.
public boolean
equals(Object
object to be compared for equality with this map
object
)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares the specified object with this map for equality.
public V
get(Object
the key whose associated value is to be returned
key
)

Implements java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Returns the hash code value for this map.
public boolean
isEmpty()

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.
public Set<K>
keySet()

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.
public V
put(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Implements java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation).
public void
putAll(Map<? extends K, ? extends V>
mappings to be stored in this map
map
)

Implements java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation).
public V
remove(Object
key whose mapping is to be removed from the map
key
)

Implements java.util.Map.remove.

Removes the mapping for a key from this map if it is present (optional operation).
public int
size()

Implements java.util.Map.size.

Returns the number of key-value mappings in this map.
protected void
standardClear()

A sensible definition of clear in terms of the iterator method of entrySet.

protected boolean
standardContainsKey(Object key)

A sensible, albeit inefficient, definition of containsKey in terms of the iterator method of entrySet.

protected boolean
standardContainsValue(Object value)

A sensible definition of containsValue in terms of the iterator method of entrySet.

protected boolean
standardEquals(Object object)

A sensible definition of equals in terms of the equals method of entrySet.

protected int
standardHashCode()

A sensible definition of hashCode in terms of the iterator method of entrySet.

protected boolean
standardIsEmpty()

A sensible definition of isEmpty in terms of the iterator method of entrySet.

protected void
standardPutAll(Map<? extends K, ? extends V> map)

A sensible definition of putAll(Map) in terms of put(Object, Object).

protected V
standardRemove(Object key)

A sensible, albeit inefficient, definition of remove in terms of the iterator method of entrySet.

protected String
standardToString()

A sensible definition of toString in terms of the iterator method of entrySet.

public Collection<V>
values()

Implements java.util.Map.values.

Returns a Collection view of the values contained in this map.
Inherited from com.google.common.collect.ForwardingObject:
toString

Constructor Detail

ForwardingMapback to summary
protected ForwardingMap()

Constructor for use by subclasses.

Method Detail

clearback to summary
public void clear()

Implements java.util.Map.clear.

Doc from java.util.Map.clear.

Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.

Annotations
@Override
containsKeyback to summary
public boolean containsKey(Object key)

Implements java.util.Map.containsKey.

Doc from java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that Objects.equals(key, k). (There can be at most one such mapping.)

Parameters
key:Object

key whose presence in this map is to be tested

Returns:boolean

true if this map contains a mapping for the specified key

Annotations
@Override
containsValueback to summary
public boolean containsValue(Object value)

Implements java.util.Map.containsValue.

Doc from java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that Objects.equals(value, v). This operation will probably require time linear in the map size for most implementations of the Map interface.

Parameters
value:Object

value whose presence in this map is to be tested

Returns:boolean

true if this map maps one or more keys to the specified value

Annotations
@Override
delegateback to summary
protected abstract Map<K, V> delegate()

Implements abstract com.google.common.collect.ForwardingObject.delegate.

Doc from com.google.common.collect.ForwardingObject.delegate.

Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as ForwardingSet#delegate. Concrete subclasses override this method to supply the instance being decorated.

Annotations
@Override
entrySetback to summary
public Set<Map.Entry<K, V>> entrySet()

Implements java.util.Map.entrySet.

Doc from java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns:Set<Map.Entry<K, V>>

a set view of the mappings contained in this map

Annotations
@Override
equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Doc from java.util.Map.equals.

Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

Parameters
object:Object

object to be compared for equality with this map

Returns:boolean

true if the specified object is equal to this map

Annotations
@Override
getback to summary
public V get(Object key)

Implements java.util.Map.get.

Doc from java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that Objects.equals(key, k), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Parameters
key:Object

the key whose associated value is to be returned

Returns:V

the value to which the specified key is mapped, or null if this map contains no mapping for the key

Annotations
@Override
@CheckForNull
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Doc from java.util.Map.hashCode.

Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view. This ensures that m1.equals(m2) implies that m1.hashCode()==m2.hashCode() for any two maps m1 and m2, as required by the general contract of Object#hashCode.

Returns:int

the hash code value for this map

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Map.isEmpty.

Doc from java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

Returns:boolean

true if this map contains no key-value mappings

Annotations
@Override
keySetback to summary
public Set<K> keySet()

Implements java.util.Map.keySet.

Doc from java.util.Map.keySet.

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns:Set<K>

a set view of the keys contained in this map

Annotations
@Override
putback to summary
public V put(K key, V value)

Implements java.util.Map.put.

Doc from java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Parameters
key:K

key with which the specified value is to be associated

value:V

value to be associated with the specified key

Returns:V

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

Annotations
@CanIgnoreReturnValue
@Override
@CheckForNull
putAllback to summary
public void putAll(Map<? extends K, ? extends V> map)

Implements java.util.Map.putAll.

Doc from java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress. If the specified map has a defined encounter order, processing of its mappings generally occurs in that order.

Parameters
map:Map<? extends K, ? extends V>

mappings to be stored in this map

Annotations
@Override
removeback to summary
public V remove(Object key)

Implements java.util.Map.remove.

Doc from java.util.Map.remove.

Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that Objects.equals(key, k), that mapping is removed. (The map can contain at most one such mapping.)

Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

If this map permits null values, then a return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.

The map will not contain a mapping for the specified key once the call returns.

Parameters
key:Object

key whose mapping is to be removed from the map

Returns:V

the previous value associated with key, or null if there was no mapping for key.

Annotations
@CanIgnoreReturnValue
@Override
@CheckForNull
sizeback to summary
public int size()

Implements java.util.Map.size.

Doc from java.util.Map.size.

Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of key-value mappings in this map

Annotations
@Override
standardClearback to summary
protected void standardClear()

A sensible definition of clear in terms of the iterator method of entrySet. In many cases, you may wish to override clear to forward to this implementation.

Since
7.0
standardContainsKeyback to summary
protected boolean standardContainsKey(Object key)

A sensible, albeit inefficient, definition of containsKey in terms of the iterator method of entrySet. If you override entrySet, you may wish to override containsKey to forward to this implementation.

Since
7.0
standardContainsValueback to summary
protected boolean standardContainsValue(Object value)

A sensible definition of containsValue in terms of the iterator method of entrySet. If you override entrySet, you may wish to override containsValue to forward to this implementation.

Since
7.0
standardEqualsback to summary
protected boolean standardEquals(Object object)

A sensible definition of equals in terms of the equals method of entrySet. If you override entrySet, you may wish to override equals to forward to this implementation.

Since
7.0
standardHashCodeback to summary
protected int standardHashCode()

A sensible definition of hashCode in terms of the iterator method of entrySet. If you override entrySet, you may wish to override hashCode to forward to this implementation.

Since
7.0
standardIsEmptyback to summary
protected boolean standardIsEmpty()

A sensible definition of isEmpty in terms of the iterator method of entrySet. If you override entrySet, you may wish to override isEmpty to forward to this implementation.

Since
7.0
standardPutAllback to summary
protected void standardPutAll(Map<? extends K, ? extends V> map)

A sensible definition of putAll(Map) in terms of put(Object, Object). If you override put(Object, Object), you may wish to override putAll(Map) to forward to this implementation.

Since
7.0
standardRemoveback to summary
protected V standardRemove(Object key)

A sensible, albeit inefficient, definition of remove in terms of the iterator method of entrySet. If you override entrySet, you may wish to override remove to forward to this implementation.

Alternately, you may wish to override remove with keySet().remove, assuming that approach would not lead to an infinite loop.

Annotations
@CheckForNull
Since
7.0
standardToStringback to summary
protected String standardToString()

A sensible definition of toString in terms of the iterator method of entrySet. If you override entrySet, you may wish to override toString to forward to this implementation.

Since
7.0
valuesback to summary
public Collection<V> values()

Implements java.util.Map.values.

Doc from java.util.Map.values.

Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns:Collection<V>

a collection view of the values contained in this map

Annotations
@Override
com.google.common.collect back to summary

protected abstract Class ForwardingMap.StandardEntrySet

extends Maps.EntrySet<K extends @Nullable Object, V extends @Nullable Object>
Class Inheritance

A sensible implementation of Map#entrySet in terms of the following methods: ForwardingMap#clear, ForwardingMap#containsKey, ForwardingMap#get, ForwardingMap#isEmpty, ForwardingMap#remove, and ForwardingMap#size. In many cases, you may wish to override entrySet to forward to this implementation or a subclass thereof.
Since
10.0

Constructor Summary

AccessConstructor and Description
protected
StandardEntrySet()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
pack-priv Map<K, V>
Inherited from com.google.common.collect.Maps.EntrySet:
clearcontainsisEmptyremoveremoveAllretainAllsize

Constructor Detail

StandardEntrySetback to summary
protected StandardEntrySet()

Constructor for use by subclasses.

Method Detail

mapback to summary
pack-priv Map<K, V> map()

Implements abstract com.google.common.collect.Maps.EntrySet.map.

Annotations
@Override
com.google.common.collect back to summary

protected Class ForwardingMap.StandardKeySet

extends Maps.KeySet<K extends @Nullable Object, V extends @Nullable Object>
Class Inheritance

A sensible implementation of Map#keySet in terms of the following methods: ForwardingMap#clear, ForwardingMap#containsKey, ForwardingMap#isEmpty, ForwardingMap#remove, ForwardingMap#size, and the Set#iterator method of ForwardingMap#entrySet. In many cases, you may wish to override ForwardingMap#keySet to forward to this implementation or a subclass thereof.
Since
10.0

Field Summary

Inherited from com.google.common.collect.Maps.KeySet:
map

Constructor Summary

AccessConstructor and Description
public
StandardKeySet()

Constructor for use by subclasses.

Method Summary

Inherited from com.google.common.collect.Maps.KeySet:
clearcontainsforEachisEmptyiteratormapremovesize

Constructor Detail

StandardKeySetback to summary
public StandardKeySet()

Constructor for use by subclasses.

com.google.common.collect back to summary

protected Class ForwardingMap.StandardValues

extends Maps.Values<K extends @Nullable Object, V extends @Nullable Object>
Class Inheritance

A sensible implementation of Map#values in terms of the following methods: ForwardingMap#clear, ForwardingMap#containsValue, ForwardingMap#isEmpty, ForwardingMap#size, and the Set#iterator method of ForwardingMap#entrySet. In many cases, you may wish to override ForwardingMap#values to forward to this implementation or a subclass thereof.
Since
10.0

Field Summary

Inherited from com.google.common.collect.Maps.Values:
map

Constructor Summary

AccessConstructor and Description
public
StandardValues()

Constructor for use by subclasses.

Method Summary

Inherited from com.google.common.collect.Maps.Values:
clearcontainsforEachisEmptyiteratormapremoveremoveAllretainAllsize

Constructor Detail

StandardValuesback to summary
public StandardValues()

Constructor for use by subclasses.