AbstractMap.java revision 2362
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. 827N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 827N/A * This code is free software; you can redistribute it and/or modify it 827N/A * under the terms of the GNU General Public License version 2 only, as 827N/A * published by the Free Software Foundation. Oracle designates this 827N/A * particular file as subject to the "Classpath" exception as provided 827N/A * by Oracle in the LICENSE file that accompanied this code. 827N/A * This code is distributed in the hope that it will be useful, but WITHOUT 827N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 827N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 827N/A * version 2 for more details (a copy is included in the LICENSE file that 827N/A * accompanied this code). 827N/A * You should have received a copy of the GNU General Public License version 827N/A * 2 along with this work; if not, write to the Free Software Foundation, 2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 827N/A * or visit www.oracle.com if you need additional information or have any 827N/A * This class provides a skeletal implementation of the <tt>Map</tt> 827N/A * interface, to minimize the effort required to implement this interface. 827N/A * <p>To implement an unmodifiable map, the programmer needs only to extend this 827N/A * class and provide an implementation for the <tt>entrySet</tt> method, which 827N/A * returns a set-view of the map's mappings. Typically, the returned set 827N/A * will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should 827N/A * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator 827N/A * should not support the <tt>remove</tt> method. 827N/A * <p>To implement a modifiable map, the programmer must additionally override 827N/A * this class's <tt>put</tt> method (which otherwise throws an 827N/A * <tt>UnsupportedOperationException</tt>), and the iterator returned by 827N/A * <tt>entrySet().iterator()</tt> must additionally implement its 827N/A * <tt>remove</tt> method. 827N/A * <p>The programmer should generally provide a void (no argument) and map 827N/A * constructor, as per the recommendation in the <tt>Map</tt> interface 827N/A * <p>The documentation for each non-abstract method in this class describes its 827N/A * implementation in detail. Each of these methods may be overridden if the 827N/A * map being implemented admits a more efficient implementation. 827N/A * <p>This class is a member of the 827N/A * Java Collections Framework</a>. 827N/A * @param <K> the type of keys maintained by this map 827N/A * @param <V> the type of mapped values 827N/A * Sole constructor. (For invocation by subclass constructors, typically 827N/A * <p>This implementation returns <tt>entrySet().size()</tt>. * <p>This implementation returns <tt>size() == 0</tt>. * <p>This implementation iterates over <tt>entrySet()</tt> searching * for an entry with the specified value. If such an entry is found, * <tt>true</tt> is returned. If the iteration terminates without * finding such an entry, <tt>false</tt> is returned. Note that this * implementation requires linear time in the size of the map. * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * <p>This implementation iterates over <tt>entrySet()</tt> searching * for an entry with the specified key. If such an entry is found, * <tt>true</tt> is returned. If the iteration terminates without * finding such an entry, <tt>false</tt> is returned. Note that this * implementation requires linear time in the size of the map; many * implementations will override this method. * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * <p>This implementation iterates over <tt>entrySet()</tt> searching * for an entry with the specified key. If such an entry is found, * the entry's value is returned. If the iteration terminates without * finding such an entry, <tt>null</tt> is returned. Note that this * implementation requires linear time in the size of the map; many * implementations will override this method. * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} // Modification Operations * <p>This implementation always throws an * <tt>UnsupportedOperationException</tt>. * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * <p>This implementation iterates over <tt>entrySet()</tt> searching for an * entry with the specified key. If such an entry is found, its value is * obtained with its <tt>getValue</tt> operation, the entry is removed * from the collection (and the backing map) with the iterator's * <tt>remove</tt> operation, and the saved value is returned. If the * iteration terminates without finding such an entry, <tt>null</tt> is * returned. Note that this implementation requires linear time in the * size of the map; many implementations will override this method. * <p>Note that this implementation throws an * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> * iterator does not support the <tt>remove</tt> method and this map * contains a mapping for the specified key. * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * <p>This implementation iterates over the specified map's * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt> * operation once for each entry returned by the iteration. * <p>Note that this implementation throws an * <tt>UnsupportedOperationException</tt> if this map does not support * the <tt>put</tt> operation and the specified map is nonempty. * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} public void putAll(
Map<?
extends K, ?
extends V> m) {
* <p>This implementation calls <tt>entrySet().clear()</tt>. * <p>Note that this implementation throws an * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> * does not support the <tt>clear</tt> operation. * @throws UnsupportedOperationException {@inheritDoc} * Each of these fields are initialized to contain an instance of the * appropriate view the first time this view is requested. The views are * stateless, so there's no reason to create more than one of each. * <p>This implementation returns a set that subclasses {@link AbstractSet}. * The subclass's iterator method returns a "wrapper object" over this * map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method * delegates to this map's <tt>size</tt> method and the * <tt>contains</tt> method delegates to this map's * <tt>containsKey</tt> method. * <p>The set is created the first time this method is called, * and returned in response to all subsequent calls. No synchronization * is performed, so there is a slight chance that multiple calls to this * method will not all return the same set. * <p>This implementation returns a collection that subclasses {@link * AbstractCollection}. The subclass's iterator method returns a * "wrapper object" over this map's <tt>entrySet()</tt> iterator. * The <tt>size</tt> method delegates to this map's <tt>size</tt> * method and the <tt>contains</tt> method delegates to this map's * <tt>containsValue</tt> method. * <p>The collection is created the first time this method is called, and * returned in response to all subsequent calls. No synchronization is * performed, so there is a slight chance that multiple calls to this * method will not all return the same collection. // Comparison and hashing * Compares the specified object with this map for equality. Returns * <tt>true</tt> if the given object is also a map and the two maps * represent the same mappings. More formally, two maps <tt>m1</tt> and * <tt>m2</tt> represent the same mappings if * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the * <tt>equals</tt> method works properly across different implementations * of the <tt>Map</tt> interface. * <p>This implementation first checks if the specified object is this map; * if so it returns <tt>true</tt>. Then, it checks if the specified * object is a map whose size is identical to the size of this map; if * not, it returns <tt>false</tt>. If so, it iterates over this map's * <tt>entrySet</tt> collection, and checks that the specified map * contains each mapping that this map contains. If the specified map * fails to contain such a mapping, <tt>false</tt> is returned. If the * iteration completes, <tt>true</tt> is returned. * @param o object to be compared for equality with this map * @return <tt>true</tt> if the specified object is equal to this map * 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 * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of * {@link Object#hashCode}. * <p>This implementation iterates over <tt>entrySet()</tt>, calling * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the * set, and adding up the results. * @return the hash code value for this map * @see Map.Entry#hashCode() * @see Object#equals(Object) * @see Set#equals(Object) * Returns a string representation of this map. The string representation * consists of a list of key-value mappings in the order returned by the * map's <tt>entrySet</tt> view's iterator, enclosed in braces * (<tt>"{}"</tt>). Adjacent mappings are separated by the characters * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as * the key followed by an equals sign (<tt>"="</tt>) followed by the * associated value. Keys and values are converted to strings as by * {@link String#valueOf(Object)}. * @return a string representation of this map * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys * and values themselves are not cloned. * @return a shallow copy of this map * Utility method for SimpleEntry and SimpleImmutableEntry. * Test for equality, checking for nulls. // Implementation Note: SimpleEntry and SimpleImmutableEntry // are distinct unrelated classes, even though they share // some code. Since you can't add or subtract final-ness // of a field in a subclass, they can't share representations, // and the amount of duplicated code is too small to warrant // exposing a common abstract class. * An Entry maintaining a key and a value. The value may be * changed using the <tt>setValue</tt> method. This class * facilitates the process of building custom map * implementations. For example, it may be convenient to return * arrays of <tt>SimpleEntry</tt> instances in method * <tt>Map.entrySet().toArray</tt>. * Creates an entry representing a mapping from the specified * key to the specified value. * @param key the key represented by this entry * @param value the value represented by this entry * Creates an entry representing the same mapping as the * @param entry the entry to copy * Returns the key corresponding to this entry. * @return the key corresponding to this entry * Returns the value corresponding to this entry. * @return the value corresponding to this entry * Replaces the value corresponding to this entry with the specified * @param value new value to be stored in this entry * @return the old value corresponding to the entry * Compares the specified object with this entry for equality. * Returns {@code true} if the given object is also a map entry and * the two entries represent the same mapping. More formally, two * entries {@code e1} and {@code e2} represent the same mapping * e1.getKey().equals(e2.getKey())) * e1.getValue().equals(e2.getValue()))</pre> * This ensures that the {@code equals} method works properly across * different implementations of the {@code Map.Entry} interface. * @param o object to be compared for equality with this map entry * @return {@code true} if the specified object is equal to this map * Returns the hash code value for this map entry. The hash code * of a map entry {@code e} is defined to be: <pre> * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> * This ensures that {@code e1.equals(e2)} implies that * {@code e1.hashCode()==e2.hashCode()} for any two Entries * {@code e1} and {@code e2}, as required by the general * contract of {@link Object#hashCode}. * @return the hash code value for this map entry * Returns a String representation of this map entry. This * implementation returns the string representation of this * entry's key followed by the equals character ("<tt>=</tt>") * followed by the string representation of this entry's value. * @return a String representation of this map entry * An Entry maintaining an immutable key and value. This class * does not support method <tt>setValue</tt>. This class may be * convenient in methods that return thread-safe snapshots of * Creates an entry representing a mapping from the specified * key to the specified value. * @param key the key represented by this entry * @param value the value represented by this entry * Creates an entry representing the same mapping as the * @param entry the entry to copy * Returns the key corresponding to this entry. * @return the key corresponding to this entry * Returns the value corresponding to this entry. * @return the value corresponding to this entry * Replaces the value corresponding to this entry with the specified * value (optional operation). This implementation simply throws * <tt>UnsupportedOperationException</tt>, as this class implements * an <i>immutable</i> map entry. * @param value new value to be stored in this entry * @return (Does not return) * @throws UnsupportedOperationException always * Compares the specified object with this entry for equality. * Returns {@code true} if the given object is also a map entry and * the two entries represent the same mapping. More formally, two * entries {@code e1} and {@code e2} represent the same mapping * e1.getKey().equals(e2.getKey())) * e1.getValue().equals(e2.getValue()))</pre> * This ensures that the {@code equals} method works properly across * different implementations of the {@code Map.Entry} interface. * @param o object to be compared for equality with this map entry * @return {@code true} if the specified object is equal to this map * Returns the hash code value for this map entry. The hash code * of a map entry {@code e} is defined to be: <pre> * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> * This ensures that {@code e1.equals(e2)} implies that * {@code e1.hashCode()==e2.hashCode()} for any two Entries * {@code e1} and {@code e2}, as required by the general * contract of {@link Object#hashCode}. * @return the hash code value for this map entry * Returns a String representation of this map entry. This * implementation returns the string representation of this * entry's key followed by the equals character ("<tt>=</tt>") * followed by the string representation of this entry's value. * @return a String representation of this map entry