Class AbstractMap<K,V>
- All Implemented Interfaces:
Map<K,V>
- Direct Known Subclasses:
ConcurrentHashMap,ConcurrentSkipListMap,EnumMap,HashMap,IdentityHashMap,TreeMap,WeakHashMap
public abstract class AbstractMap<K,V> extends Object implements Map<K,V>
Map implementations.
Subclasses that permit new mappings to be added must override put(K, V).
The default implementations of many methods are inefficient for large
maps. For example in the default implementation, each call to get(java.lang.Object)
performs a linear iteration of the entry set. Subclasses should override such
methods to improve their performance.
- Since:
- 1.2
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractMap.SimpleEntry<K,V>A key-value mapping with mutable values.static classAbstractMap.SimpleImmutableEntry<K,V>An immutable key-value mapping. -
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractMap() -
Method Summary
Modifier and Type Method Description voidclear()Removes all elements from thisMap, leaving it empty.protected Objectclone()Creates and returns a copy of thisObject.booleancontainsKey(Object key)Returns whether thisMapcontains the specified key.booleancontainsValue(Object value)Returns whether thisMapcontains the specified value.abstract Set<Map.Entry<K,V>>entrySet()Returns aSetcontaining all of the mappings in thisMap.booleanequals(Object object)Compares this instance with the specified object and indicates if they are equal.Vget(Object key)Returns the value of the mapping with the specified key.inthashCode()Returns an integer hash code for this object.booleanisEmpty()Returns whether this map is empty.Set<K>keySet()Returns a set of the keys contained in thisMap.Vput(K key, V value)Maps the specified key to the specified value.voidputAll(Map<? extends K,? extends V> map)Copies every mapping in the specifiedMapto thisMap.Vremove(Object key)Removes a mapping with the specified key from thisMap.intsize()Returns the number of mappings in thisMap.StringtoString()Returns a string containing a concise, human-readable description of this object.Collection<V>values()Returns aCollectionof the values contained in thisMap.
-
Constructor Details
-
AbstractMap
protected AbstractMap()
-
-
Method Details
-
clear
public void clear()Removes all elements from thisMap, leaving it empty.This implementation calls
entrySet().clear().- Specified by:
clearin interfaceMap<K,V>- See Also:
Map.isEmpty(),Map.size()
-
containsKey
Returns whether thisMapcontains the specified key.This implementation iterates its key set, looking for a key that
keyequals.- Specified by:
containsKeyin interfaceMap<K,V>- Parameters:
key- the key to search for.- Returns:
trueif this map contains the specified key,falseotherwise.
-
containsValue
Returns whether thisMapcontains the specified value.This implementation iterates its entry set, looking for an entry with a value that
valueequals.- Specified by:
containsValuein interfaceMap<K,V>- Parameters:
value- the value to search for.- Returns:
trueif this map contains the specified value,falseotherwise.
-
entrySet
Description copied from interface:MapReturns aSetcontaining all of the mappings in thisMap. Each mapping is an instance ofMap.Entry. As theSetis backed by thisMap, changes in one will be reflected in the other. -
equals
Compares this instance with the specified object and indicates if they are equal. In order to be equal,omust represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.The default implementation returns
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually override either both methods or neither of them.This implementation first checks the structure of
object. If it is not a map or of a different size, this returns false. Otherwise it iterates its own entry set, looking up each entry's key inobject. If any value does not equal the other map's value for the same key, this returns false. Otherwise it returns true. -
get
Returns the value of the mapping with the specified key.This implementation iterates its entry set, looking for an entry with a key that
keyequals. -
hashCode
public int hashCode()Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.This implementation iterates its entry set, summing the hashcodes of its entries.
-
isEmpty
public boolean isEmpty()Returns whether this map is empty.This implementation compares
size()to 0.- Specified by:
isEmptyin interfaceMap<K,V>- Returns:
trueif this map has no elements,falseotherwise.- See Also:
Map.size()
-
keySet
Returns a set of the keys contained in thisMap. TheSetis backed by thisMapso changes to one are reflected by the other. TheSetdoes not support adding.This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return keys.
-
put
Maps the specified key to the specified value.This base implementation throws
UnsupportedOperationException. -
putAll
Copies every mapping in the specifiedMapto thisMap.This implementation iterates through
map's entry set, callingput()for each. -
remove
Removes a mapping with the specified key from thisMap.This implementation iterates its entry set, removing the entry with a key that
keyequals. -
size
public int size()Returns the number of mappings in thisMap.This implementation returns its entry set's size.
-
toString
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.This implementation composes a string by iterating its entry set. If this map contains itself as a key or a value, the string "(this Map)" will appear in its place.
-
values
Returns aCollectionof the values contained in thisMap. TheCollectionis backed by thisMapso changes to one are reflected by the other. TheCollectionsupportsCollection.remove(java.lang.Object),Collection.removeAll(java.util.Collection<?>),Collection.retainAll(java.util.Collection<?>), andCollection.clear()operations, and it does not supportCollection.add(E)orCollection.addAll(java.util.Collection<? extends E>)operations.This method returns a
Collectionwhich is the subclass ofAbstractCollection. TheAbstractCollection.iterator()method of this subclass returns a "wrapper object" over the iterator of thisMap'sMap.entrySet(). TheAbstractCollection.size()method wraps thisMap'sMap.size()method and theAbstractCollection.contains(java.lang.Object)method wraps thisMap'sMap.containsValue(java.lang.Object)method.The collection is created when this method is called at first time and returned in response to all subsequent calls. This method may return different Collection when multiple calls to this method, since it has no synchronization performed.
This implementation returns a view that calls through this to map. Its iterator transforms this map's entry set iterator to return values.
-
clone
Description copied from class:ObjectCreates and returns a copy of thisObject. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should callsuper.clone()to create the new instance and then create deep copies of the nested, mutable objects.- Overrides:
clonein classObject- Returns:
- a copy of this object.
- Throws:
CloneNotSupportedException- if this object's class does not implement theCloneableinterface.
-