6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
+ |
import java.util.*; // for javadoc (till 6280605 is fixed) |
10 |
|
import java.util.Map.Entry; |
11 |
|
|
12 |
|
/** |
86 |
|
* finding such an entry, <tt>false</tt> is returned. Note that this |
87 |
|
* implementation requires linear time in the size of the map. |
88 |
|
* |
88 |
– |
* @throws NullPointerException {@inheritDoc} |
89 |
|
* @throws ClassCastException {@inheritDoc} |
90 |
+ |
* @throws NullPointerException {@inheritDoc} |
91 |
|
*/ |
92 |
|
public boolean containsValue(Object value) { |
93 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
117 |
|
* implementation requires linear time in the size of the map; many |
118 |
|
* implementations will override this method. |
119 |
|
* |
119 |
– |
* @throws NullPointerException {@inheritDoc} |
120 |
|
* @throws ClassCastException {@inheritDoc} |
121 |
+ |
* @throws NullPointerException {@inheritDoc} |
122 |
|
*/ |
123 |
|
public boolean containsKey(Object key) { |
124 |
|
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
148 |
|
* implementation requires linear time in the size of the map; many |
149 |
|
* implementations will override this method. |
150 |
|
* |
150 |
– |
* @throws NullPointerException {@inheritDoc} |
151 |
|
* @throws ClassCastException {@inheritDoc} |
152 |
+ |
* @throws NullPointerException {@inheritDoc} |
153 |
|
*/ |
154 |
|
public V get(Object key) { |
155 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
179 |
|
* <tt>UnsupportedOperationException</tt>. |
180 |
|
* |
181 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
181 |
– |
* @throws NullPointerException {@inheritDoc} |
182 |
|
* @throws ClassCastException {@inheritDoc} |
183 |
+ |
* @throws NullPointerException {@inheritDoc} |
184 |
|
* @throws IllegalArgumentException {@inheritDoc} |
185 |
|
*/ |
186 |
|
public V put(K key, V value) { |
205 |
|
* contains a mapping for the specified key. |
206 |
|
* |
207 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
207 |
– |
* @throws NullPointerException {@inheritDoc} |
208 |
|
* @throws ClassCastException {@inheritDoc} |
209 |
+ |
* @throws NullPointerException {@inheritDoc} |
210 |
|
*/ |
211 |
|
public V remove(Object key) { |
212 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
248 |
|
* the <tt>put</tt> operation and the specified map is nonempty. |
249 |
|
* |
250 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
250 |
– |
* @throws NullPointerException {@inheritDoc} |
251 |
|
* @throws ClassCastException {@inheritDoc} |
252 |
+ |
* @throws NullPointerException {@inheritDoc} |
253 |
|
* @throws IllegalArgumentException {@inheritDoc} |
254 |
|
*/ |
255 |
|
public void putAll(Map<? extends K, ? extends V> m) { |
388 |
|
// Comparison and hashing |
389 |
|
|
390 |
|
/** |
391 |
< |
* {@inheritDoc} |
391 |
> |
* Compares the specified object with this map for equality. Returns |
392 |
> |
* <tt>true</tt> if the given object is also a map and the two maps |
393 |
> |
* represent the same mappings. More formally, two maps <tt>m1</tt> and |
394 |
> |
* <tt>m2</tt> represent the same mappings if |
395 |
> |
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the |
396 |
> |
* <tt>equals</tt> method works properly across different implementations |
397 |
> |
* of the <tt>Map</tt> interface. |
398 |
|
* |
399 |
|
* <p>This implementation first checks if the specified object is this map; |
400 |
|
* if so it returns <tt>true</tt>. Then, it checks if the specified |
404 |
|
* contains each mapping that this map contains. If the specified map |
405 |
|
* fails to contain such a mapping, <tt>false</tt> is returned. If the |
406 |
|
* iteration completes, <tt>true</tt> is returned. |
407 |
+ |
* |
408 |
+ |
* @param o object to be compared for equality with this map |
409 |
+ |
* @return <tt>true</tt> if the specified object is equal to this map |
410 |
|
*/ |
411 |
|
public boolean equals(Object o) { |
412 |
|
if (o == this) |
414 |
|
|
415 |
|
if (!(o instanceof Map)) |
416 |
|
return false; |
417 |
< |
Map<K,V> t = (Map<K,V>) o; |
418 |
< |
if (t.size() != size()) |
417 |
> |
Map<K,V> m = (Map<K,V>) o; |
418 |
> |
if (m.size() != size()) |
419 |
|
return false; |
420 |
|
|
421 |
|
try { |
425 |
|
K key = e.getKey(); |
426 |
|
V value = e.getValue(); |
427 |
|
if (value == null) { |
428 |
< |
if (!(t.get(key)==null && t.containsKey(key))) |
428 |
> |
if (!(m.get(key)==null && m.containsKey(key))) |
429 |
|
return false; |
430 |
|
} else { |
431 |
< |
if (!value.equals(t.get(key))) |
431 |
> |
if (!value.equals(m.get(key))) |
432 |
|
return false; |
433 |
|
} |
434 |
|
} |
442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
< |
* {@inheritDoc} |
445 |
> |
* Returns the hash code value for this map. The hash code of a map is |
446 |
> |
* defined to be the sum of the hash codes of each entry in the map's |
447 |
> |
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> |
448 |
> |
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps |
449 |
> |
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of |
450 |
> |
* {@link Object#hashCode}. |
451 |
|
* |
452 |
|
* <p>This implementation iterates over <tt>entrySet()</tt>, calling |
453 |
< |
* <tt>hashCode()</tt> on each element (entry) in the set, and |
454 |
< |
* adding up the results. |
453 |
> |
* {@link Map.Entry#hashCode hashCode()} on each element (entry) in the |
454 |
> |
* set, and adding up the results. |
455 |
|
* |
456 |
+ |
* @return the hash code value for this map |
457 |
|
* @see Map.Entry#hashCode() |
442 |
– |
* @see Object#hashCode() |
458 |
|
* @see Object#equals(Object) |
459 |
|
* @see Set#equals(Object) |
460 |
|
*/ |
483 |
|
* appended. Finally a right brace is appended. A string is obtained |
484 |
|
* from the stringbuffer, and returned. |
485 |
|
* |
486 |
< |
* @return a String representation of this map. |
486 |
> |
* @return a String representation of this map |
487 |
|
*/ |
488 |
|
public String toString() { |
489 |
< |
StringBuffer buf = new StringBuffer(); |
490 |
< |
buf.append("{"); |
489 |
> |
StringBuilder sb = new StringBuilder(); |
490 |
> |
sb.append("{"); |
491 |
|
|
492 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
493 |
|
boolean hasNext = i.hasNext(); |
496 |
|
K key = e.getKey(); |
497 |
|
V value = e.getValue(); |
498 |
|
if (key == this) |
499 |
< |
buf.append("(this Map)"); |
499 |
> |
sb.append("(this Map)"); |
500 |
|
else |
501 |
< |
buf.append(key); |
502 |
< |
buf.append("="); |
501 |
> |
sb.append(key); |
502 |
> |
sb.append("="); |
503 |
|
if (value == this) |
504 |
< |
buf.append("(this Map)"); |
504 |
> |
sb.append("(this Map)"); |
505 |
|
else |
506 |
< |
buf.append(value); |
506 |
> |
sb.append(value); |
507 |
|
hasNext = i.hasNext(); |
508 |
|
if (hasNext) |
509 |
< |
buf.append(", "); |
509 |
> |
sb.append(", "); |
510 |
|
} |
511 |
|
|
512 |
< |
buf.append("}"); |
513 |
< |
return buf.toString(); |
512 |
> |
sb.append("}"); |
513 |
> |
return sb.toString(); |
514 |
|
} |
515 |
|
|
516 |
|
/** |
531 |
|
* Test for equality, checking for nulls. |
532 |
|
*/ |
533 |
|
private static boolean eq(Object o1, Object o2) { |
534 |
< |
return (o1 == null ? o2 == null : o1.equals(o2)); |
534 |
> |
return o1 == null ? o2 == null : o1.equals(o2); |
535 |
|
} |
536 |
|
|
537 |
|
// Implementation Note: SimpleEntry and SimpleImmutableEntry |
548 |
|
* facilitates the process of building custom map |
549 |
|
* implementations. For example, it may be convenient to return |
550 |
|
* arrays of <tt>SimpleEntry</tt> instances in method |
551 |
< |
* <tt>Map.entrySet().toArray</tt> |
551 |
> |
* <tt>Map.entrySet().toArray</tt>. |
552 |
> |
* |
553 |
> |
* @since 1.6 |
554 |
|
*/ |
555 |
< |
public static class SimpleEntry<K,V> implements Entry<K,V> { |
555 |
> |
public static class SimpleEntry<K,V> |
556 |
> |
implements Entry<K,V>, java.io.Serializable |
557 |
> |
{ |
558 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
559 |
> |
|
560 |
|
private final K key; |
561 |
|
private V value; |
562 |
|
|
576 |
|
* Creates an entry representing the same mapping as the |
577 |
|
* specified entry. |
578 |
|
* |
579 |
< |
* @param entry the entry to copy. |
579 |
> |
* @param entry the entry to copy |
580 |
|
*/ |
581 |
|
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
582 |
|
this.key = entry.getKey(); |
622 |
|
} |
623 |
|
|
624 |
|
public int hashCode() { |
625 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
626 |
< |
((value == null) ? 0 : value.hashCode()); |
625 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
626 |
> |
(value == null ? 0 : value.hashCode()); |
627 |
|
} |
628 |
|
|
629 |
|
/** |
641 |
|
} |
642 |
|
|
643 |
|
/** |
644 |
< |
* An Entry maintaining an immutable key and value, This class |
644 |
> |
* An Entry maintaining an immutable key and value. This class |
645 |
|
* does not support method <tt>setValue</tt>. This class may be |
646 |
|
* convenient in methods that return thread-safe snapshots of |
647 |
|
* key-value mappings. |
648 |
+ |
* |
649 |
+ |
* @since 1.6 |
650 |
|
*/ |
651 |
< |
public static class SimpleImmutableEntry<K,V> implements Entry<K,V> { |
651 |
> |
public static class SimpleImmutableEntry<K,V> |
652 |
> |
implements Entry<K,V>, java.io.Serializable |
653 |
> |
{ |
654 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
655 |
> |
|
656 |
|
private final K key; |
657 |
|
private final V value; |
658 |
|
|
719 |
|
} |
720 |
|
|
721 |
|
public int hashCode() { |
722 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
723 |
< |
((value == null) ? 0 : value.hashCode()); |
722 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
723 |
> |
(value == null ? 0 : value.hashCode()); |
724 |
|
} |
725 |
|
|
726 |
|
/** |