2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 import java.util.Map.Entry;
30 * This class provides a skeletal implementation of the <tt>Map</tt>
31 * interface, to minimize the effort required to implement this interface.
33 * <p>To implement an unmodifiable map, the programmer needs only to extend this
34 * class and provide an implementation for the <tt>entrySet</tt> method, which
35 * returns a set-view of the map's mappings. Typically, the returned set
36 * will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should
37 * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator
38 * should not support the <tt>remove</tt> method.
40 * <p>To implement a modifiable map, the programmer must additionally override
41 * this class's <tt>put</tt> method (which otherwise throws an
42 * <tt>UnsupportedOperationException</tt>), and the iterator returned by
43 * <tt>entrySet().iterator()</tt> must additionally implement its
44 * <tt>remove</tt> method.
46 * <p>The programmer should generally provide a void (no argument) and map
47 * constructor, as per the recommendation in the <tt>Map</tt> interface
50 * <p>The documentation for each non-abstract method in this class describes its
51 * implementation in detail. Each of these methods may be overridden if the
52 * map being implemented admits a more efficient implementation.
54 * <p>This class is a member of the
55 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
56 * Java Collections Framework</a>.
58 * @param <K> the type of keys maintained by this map
59 * @param <V> the type of mapped values
68 public abstract class AbstractMap<K,V> implements Map<K,V> {
70 * Sole constructor. (For invocation by subclass constructors, typically
73 protected AbstractMap() {
81 * <p>This implementation returns <tt>entrySet().size()</tt>.
84 return entrySet().size();
90 * <p>This implementation returns <tt>size() == 0</tt>.
92 public boolean isEmpty() {
99 * <p>This implementation iterates over <tt>entrySet()</tt> searching
100 * for an entry with the specified value. If such an entry is found,
101 * <tt>true</tt> is returned. If the iteration terminates without
102 * finding such an entry, <tt>false</tt> is returned. Note that this
103 * implementation requires linear time in the size of the map.
105 * @throws ClassCastException {@inheritDoc}
106 * @throws NullPointerException {@inheritDoc}
108 public boolean containsValue(Object value) {
109 Iterator<Entry<K,V>> i = entrySet().iterator();
111 while (i.hasNext()) {
112 Entry<K,V> e = i.next();
113 if (e.getValue()==null)
117 while (i.hasNext()) {
118 Entry<K,V> e = i.next();
119 if (value.equals(e.getValue()))
129 * <p>This implementation iterates over <tt>entrySet()</tt> searching
130 * for an entry with the specified key. If such an entry is found,
131 * <tt>true</tt> is returned. If the iteration terminates without
132 * finding such an entry, <tt>false</tt> is returned. Note that this
133 * implementation requires linear time in the size of the map; many
134 * implementations will override this method.
136 * @throws ClassCastException {@inheritDoc}
137 * @throws NullPointerException {@inheritDoc}
139 public boolean containsKey(Object key) {
140 Iterator<Map.Entry<K,V>> i = entrySet().iterator();
142 while (i.hasNext()) {
143 Entry<K,V> e = i.next();
144 if (e.getKey()==null)
148 while (i.hasNext()) {
149 Entry<K,V> e = i.next();
150 if (key.equals(e.getKey()))
160 * <p>This implementation iterates over <tt>entrySet()</tt> searching
161 * for an entry with the specified key. If such an entry is found,
162 * the entry's value is returned. If the iteration terminates without
163 * finding such an entry, <tt>null</tt> is returned. Note that this
164 * implementation requires linear time in the size of the map; many
165 * implementations will override this method.
167 * @throws ClassCastException {@inheritDoc}
168 * @throws NullPointerException {@inheritDoc}
170 public V get(Object key) {
171 Iterator<Entry<K,V>> i = entrySet().iterator();
173 while (i.hasNext()) {
174 Entry<K,V> e = i.next();
175 if (e.getKey()==null)
179 while (i.hasNext()) {
180 Entry<K,V> e = i.next();
181 if (key.equals(e.getKey()))
189 // Modification Operations
194 * <p>This implementation always throws an
195 * <tt>UnsupportedOperationException</tt>.
197 * @throws UnsupportedOperationException {@inheritDoc}
198 * @throws ClassCastException {@inheritDoc}
199 * @throws NullPointerException {@inheritDoc}
200 * @throws IllegalArgumentException {@inheritDoc}
202 public V put(K key, V value) {
203 throw new UnsupportedOperationException();
209 * <p>This implementation iterates over <tt>entrySet()</tt> searching for an
210 * entry with the specified key. If such an entry is found, its value is
211 * obtained with its <tt>getValue</tt> operation, the entry is removed
212 * from the collection (and the backing map) with the iterator's
213 * <tt>remove</tt> operation, and the saved value is returned. If the
214 * iteration terminates without finding such an entry, <tt>null</tt> is
215 * returned. Note that this implementation requires linear time in the
216 * size of the map; many implementations will override this method.
218 * <p>Note that this implementation throws an
219 * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
220 * iterator does not support the <tt>remove</tt> method and this map
221 * contains a mapping for the specified key.
223 * @throws UnsupportedOperationException {@inheritDoc}
224 * @throws ClassCastException {@inheritDoc}
225 * @throws NullPointerException {@inheritDoc}
227 public V remove(Object key) {
228 Iterator<Entry<K,V>> i = entrySet().iterator();
229 Entry<K,V> correctEntry = null;
231 while (correctEntry==null && i.hasNext()) {
232 Entry<K,V> e = i.next();
233 if (e.getKey()==null)
237 while (correctEntry==null && i.hasNext()) {
238 Entry<K,V> e = i.next();
239 if (key.equals(e.getKey()))
245 if (correctEntry !=null) {
246 oldValue = correctEntry.getValue();
258 * <p>This implementation iterates over the specified map's
259 * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>
260 * operation once for each entry returned by the iteration.
262 * <p>Note that this implementation throws an
263 * <tt>UnsupportedOperationException</tt> if this map does not support
264 * the <tt>put</tt> operation and the specified map is nonempty.
266 * @throws UnsupportedOperationException {@inheritDoc}
267 * @throws ClassCastException {@inheritDoc}
268 * @throws NullPointerException {@inheritDoc}
269 * @throws IllegalArgumentException {@inheritDoc}
271 public void putAll(Map<? extends K, ? extends V> m) {
272 for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
273 put(e.getKey(), e.getValue());
279 * <p>This implementation calls <tt>entrySet().clear()</tt>.
281 * <p>Note that this implementation throws an
282 * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
283 * does not support the <tt>clear</tt> operation.
285 * @throws UnsupportedOperationException {@inheritDoc}
287 public void clear() {
295 * Each of these fields are initialized to contain an instance of the
296 * appropriate view the first time this view is requested. The views are
297 * stateless, so there's no reason to create more than one of each.
299 transient volatile Set<K> keySet = null;
300 transient volatile Collection<V> values = null;
305 * <p>This implementation returns a set that subclasses {@link AbstractSet}.
306 * The subclass's iterator method returns a "wrapper object" over this
307 * map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method
308 * delegates to this map's <tt>size</tt> method and the
309 * <tt>contains</tt> method delegates to this map's
310 * <tt>containsKey</tt> method.
312 * <p>The set is created the first time this method is called,
313 * and returned in response to all subsequent calls. No synchronization
314 * is performed, so there is a slight chance that multiple calls to this
315 * method will not all return the same set.
317 public Set<K> keySet() {
318 if (keySet == null) {
319 keySet = new AbstractSet<K>() {
320 public Iterator<K> iterator() {
321 return new Iterator<K>() {
322 private Iterator<Entry<K,V>> i = entrySet().iterator();
324 public boolean hasNext() {
329 return i.next().getKey();
332 public void remove() {
339 return AbstractMap.this.size();
342 public boolean isEmpty() {
343 return AbstractMap.this.isEmpty();
346 public void clear() {
347 AbstractMap.this.clear();
350 public boolean contains(Object k) {
351 return AbstractMap.this.containsKey(k);
361 * <p>This implementation returns a collection that subclasses {@link
362 * AbstractCollection}. The subclass's iterator method returns a
363 * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
364 * The <tt>size</tt> method delegates to this map's <tt>size</tt>
365 * method and the <tt>contains</tt> method delegates to this map's
366 * <tt>containsValue</tt> method.
368 * <p>The collection is created the first time this method is called, and
369 * returned in response to all subsequent calls. No synchronization is
370 * performed, so there is a slight chance that multiple calls to this
371 * method will not all return the same collection.
373 public Collection<V> values() {
374 if (values == null) {
375 values = new AbstractCollection<V>() {
376 public Iterator<V> iterator() {
377 return new Iterator<V>() {
378 private Iterator<Entry<K,V>> i = entrySet().iterator();
380 public boolean hasNext() {
385 return i.next().getValue();
388 public void remove() {
395 return AbstractMap.this.size();
398 public boolean isEmpty() {
399 return AbstractMap.this.isEmpty();
402 public void clear() {
403 AbstractMap.this.clear();
406 public boolean contains(Object v) {
407 return AbstractMap.this.containsValue(v);
414 public abstract Set<Entry<K,V>> entrySet();
417 // Comparison and hashing
420 * Compares the specified object with this map for equality. Returns
421 * <tt>true</tt> if the given object is also a map and the two maps
422 * represent the same mappings. More formally, two maps <tt>m1</tt> and
423 * <tt>m2</tt> represent the same mappings if
424 * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the
425 * <tt>equals</tt> method works properly across different implementations
426 * of the <tt>Map</tt> interface.
428 * <p>This implementation first checks if the specified object is this map;
429 * if so it returns <tt>true</tt>. Then, it checks if the specified
430 * object is a map whose size is identical to the size of this map; if
431 * not, it returns <tt>false</tt>. If so, it iterates over this map's
432 * <tt>entrySet</tt> collection, and checks that the specified map
433 * contains each mapping that this map contains. If the specified map
434 * fails to contain such a mapping, <tt>false</tt> is returned. If the
435 * iteration completes, <tt>true</tt> is returned.
437 * @param o object to be compared for equality with this map
438 * @return <tt>true</tt> if the specified object is equal to this map
440 public boolean equals(Object o) {
444 if (!(o instanceof Map))
446 Map<K,V> m = (Map<K,V>) o;
447 if (m.size() != size())
451 Iterator<Entry<K,V>> i = entrySet().iterator();
452 while (i.hasNext()) {
453 Entry<K,V> e = i.next();
455 V value = e.getValue();
457 if (!(m.get(key)==null && m.containsKey(key)))
460 if (!value.equals(m.get(key)))
464 } catch (ClassCastException unused) {
466 } catch (NullPointerException unused) {
474 * Returns the hash code value for this map. The hash code of a map is
475 * defined to be the sum of the hash codes of each entry in the map's
476 * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>
477 * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
478 * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
479 * {@link Object#hashCode}.
481 * <p>This implementation iterates over <tt>entrySet()</tt>, calling
482 * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
483 * set, and adding up the results.
485 * @return the hash code value for this map
486 * @see Map.Entry#hashCode()
487 * @see Object#equals(Object)
488 * @see Set#equals(Object)
490 public int hashCode() {
492 Iterator<Entry<K,V>> i = entrySet().iterator();
494 h += i.next().hashCode();
499 * Returns a string representation of this map. The string representation
500 * consists of a list of key-value mappings in the order returned by the
501 * map's <tt>entrySet</tt> view's iterator, enclosed in braces
502 * (<tt>"{}"</tt>). Adjacent mappings are separated by the characters
503 * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as
504 * the key followed by an equals sign (<tt>"="</tt>) followed by the
505 * associated value. Keys and values are converted to strings as by
506 * {@link String#valueOf(Object)}.
508 * @return a string representation of this map
510 public String toString() {
511 Iterator<Entry<K,V>> i = entrySet().iterator();
515 StringBuilder sb = new StringBuilder();
518 Entry<K,V> e = i.next();
520 V value = e.getValue();
521 sb.append(key == this ? "(this Map)" : key);
523 sb.append(value == this ? "(this Map)" : value);
525 return sb.append('}').toString();
526 sb.append(',').append(' ');
531 * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
532 * and values themselves are not cloned.
534 * @return a shallow copy of this map
536 protected Object clone() throws CloneNotSupportedException {
537 AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
538 result.keySet = null;
539 result.values = null;
544 * Utility method for SimpleEntry and SimpleImmutableEntry.
545 * Test for equality, checking for nulls.
547 private static boolean eq(Object o1, Object o2) {
548 return o1 == null ? o2 == null : o1.equals(o2);
551 // Implementation Note: SimpleEntry and SimpleImmutableEntry
552 // are distinct unrelated classes, even though they share
553 // some code. Since you can't add or subtract final-ness
554 // of a field in a subclass, they can't share representations,
555 // and the amount of duplicated code is too small to warrant
556 // exposing a common abstract class.
560 * An Entry maintaining a key and a value. The value may be
561 * changed using the <tt>setValue</tt> method. This class
562 * facilitates the process of building custom map
563 * implementations. For example, it may be convenient to return
564 * arrays of <tt>SimpleEntry</tt> instances in method
565 * <tt>Map.entrySet().toArray</tt>.
569 public static class SimpleEntry<K,V>
570 implements Entry<K,V>, java.io.Serializable
572 private static final long serialVersionUID = -8499721149061103585L;
578 * Creates an entry representing a mapping from the specified
579 * key to the specified value.
581 * @param key the key represented by this entry
582 * @param value the value represented by this entry
584 public SimpleEntry(K key, V value) {
590 * Creates an entry representing the same mapping as the
593 * @param entry the entry to copy
595 public SimpleEntry(Entry<? extends K, ? extends V> entry) {
596 this.key = entry.getKey();
597 this.value = entry.getValue();
601 * Returns the key corresponding to this entry.
603 * @return the key corresponding to this entry
610 * Returns the value corresponding to this entry.
612 * @return the value corresponding to this entry
614 public V getValue() {
619 * Replaces the value corresponding to this entry with the specified
622 * @param value new value to be stored in this entry
623 * @return the old value corresponding to the entry
625 public V setValue(V value) {
626 V oldValue = this.value;
632 * Compares the specified object with this entry for equality.
633 * Returns {@code true} if the given object is also a map entry and
634 * the two entries represent the same mapping. More formally, two
635 * entries {@code e1} and {@code e2} represent the same mapping
637 * (e1.getKey()==null ?
638 * e2.getKey()==null :
639 * e1.getKey().equals(e2.getKey()))
641 * (e1.getValue()==null ?
642 * e2.getValue()==null :
643 * e1.getValue().equals(e2.getValue()))</pre>
644 * This ensures that the {@code equals} method works properly across
645 * different implementations of the {@code Map.Entry} interface.
647 * @param o object to be compared for equality with this map entry
648 * @return {@code true} if the specified object is equal to this map
652 public boolean equals(Object o) {
653 if (!(o instanceof Map.Entry))
655 Map.Entry e = (Map.Entry)o;
656 return eq(key, e.getKey()) && eq(value, e.getValue());
660 * Returns the hash code value for this map entry. The hash code
661 * of a map entry {@code e} is defined to be: <pre>
662 * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
663 * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
664 * This ensures that {@code e1.equals(e2)} implies that
665 * {@code e1.hashCode()==e2.hashCode()} for any two Entries
666 * {@code e1} and {@code e2}, as required by the general
667 * contract of {@link Object#hashCode}.
669 * @return the hash code value for this map entry
672 public int hashCode() {
673 return (key == null ? 0 : key.hashCode()) ^
674 (value == null ? 0 : value.hashCode());
678 * Returns a String representation of this map entry. This
679 * implementation returns the string representation of this
680 * entry's key followed by the equals character ("<tt>=</tt>")
681 * followed by the string representation of this entry's value.
683 * @return a String representation of this map entry
685 public String toString() {
686 return key + "=" + value;
692 * An Entry maintaining an immutable key and value. This class
693 * does not support method <tt>setValue</tt>. This class may be
694 * convenient in methods that return thread-safe snapshots of
695 * key-value mappings.
699 public static class SimpleImmutableEntry<K,V>
700 implements Entry<K,V>, java.io.Serializable
702 private static final long serialVersionUID = 7138329143949025153L;
705 private final V value;
708 * Creates an entry representing a mapping from the specified
709 * key to the specified value.
711 * @param key the key represented by this entry
712 * @param value the value represented by this entry
714 public SimpleImmutableEntry(K key, V value) {
720 * Creates an entry representing the same mapping as the
723 * @param entry the entry to copy
725 public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
726 this.key = entry.getKey();
727 this.value = entry.getValue();
731 * Returns the key corresponding to this entry.
733 * @return the key corresponding to this entry
740 * Returns the value corresponding to this entry.
742 * @return the value corresponding to this entry
744 public V getValue() {
749 * Replaces the value corresponding to this entry with the specified
750 * value (optional operation). This implementation simply throws
751 * <tt>UnsupportedOperationException</tt>, as this class implements
752 * an <i>immutable</i> map entry.
754 * @param value new value to be stored in this entry
755 * @return (Does not return)
756 * @throws UnsupportedOperationException always
758 public V setValue(V value) {
759 throw new UnsupportedOperationException();
763 * Compares the specified object with this entry for equality.
764 * Returns {@code true} if the given object is also a map entry and
765 * the two entries represent the same mapping. More formally, two
766 * entries {@code e1} and {@code e2} represent the same mapping
768 * (e1.getKey()==null ?
769 * e2.getKey()==null :
770 * e1.getKey().equals(e2.getKey()))
772 * (e1.getValue()==null ?
773 * e2.getValue()==null :
774 * e1.getValue().equals(e2.getValue()))</pre>
775 * This ensures that the {@code equals} method works properly across
776 * different implementations of the {@code Map.Entry} interface.
778 * @param o object to be compared for equality with this map entry
779 * @return {@code true} if the specified object is equal to this map
783 public boolean equals(Object o) {
784 if (!(o instanceof Map.Entry))
786 Map.Entry e = (Map.Entry)o;
787 return eq(key, e.getKey()) && eq(value, e.getValue());
791 * Returns the hash code value for this map entry. The hash code
792 * of a map entry {@code e} is defined to be: <pre>
793 * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
794 * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
795 * This ensures that {@code e1.equals(e2)} implies that
796 * {@code e1.hashCode()==e2.hashCode()} for any two Entries
797 * {@code e1} and {@code e2}, as required by the general
798 * contract of {@link Object#hashCode}.
800 * @return the hash code value for this map entry
803 public int hashCode() {
804 return (key == null ? 0 : key.hashCode()) ^
805 (value == null ? 0 : value.hashCode());
809 * Returns a String representation of this map entry. This
810 * implementation returns the string representation of this
811 * entry's key followed by the equals character ("<tt>=</tt>")
812 * followed by the string representation of this entry's value.
814 * @return a String representation of this map entry
816 public String toString() {
817 return key + "=" + value;