1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
– |
import java.util.*; // for javadoc (till 6280605 is fixed) |
9 |
|
import java.util.Map.Entry; |
10 |
|
|
11 |
|
/** |
29 |
|
* constructor, as per the recommendation in the <tt>Map</tt> interface |
30 |
|
* specification. |
31 |
|
* |
32 |
< |
* <p>The documentation for each non-abstract methods in this class describes its |
32 |
> |
* <p>The documentation for each non-abstract method in this class describes its |
33 |
|
* implementation in detail. Each of these methods may be overridden if the |
34 |
|
* map being implemented admits a more efficient implementation. |
35 |
|
* |
36 |
|
* <p>This class is a member of the |
37 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
37 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
38 |
|
* Java Collections Framework</a>. |
39 |
|
* |
40 |
|
* @param <K> the type of keys maintained by this map |
252 |
|
* @throws IllegalArgumentException {@inheritDoc} |
253 |
|
*/ |
254 |
|
public void putAll(Map<? extends K, ? extends V> m) { |
255 |
< |
Iterator<? extends Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); |
256 |
< |
while (i.hasNext()) { |
258 |
< |
Entry<? extends K, ? extends V> e = i.next(); |
259 |
< |
put(e.getKey(), e.getValue()); |
260 |
< |
} |
255 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
256 |
> |
put(e.getKey(), e.getValue()); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
470 |
|
* <tt>", "</tt> (comma and space). Each key-value mapping is rendered as |
471 |
|
* the key followed by an equals sign (<tt>"="</tt>) followed by the |
472 |
|
* associated value. Keys and values are converted to strings as by |
473 |
< |
* <tt>String.valueOf(Object)</tt>.<p> |
478 |
< |
* |
479 |
< |
* This implementation creates an empty string buffer, appends a left |
480 |
< |
* brace, and iterates over the map's <tt>entrySet</tt> view, appending |
481 |
< |
* the string representation of each <tt>map.entry</tt> in turn. After |
482 |
< |
* appending each entry except the last, the string <tt>", "</tt> is |
483 |
< |
* appended. Finally a right brace is appended. A string is obtained |
484 |
< |
* from the stringbuffer, and returned. |
473 |
> |
* {@link String#valueOf(Object)}. |
474 |
|
* |
475 |
< |
* @return a String representation of this map |
475 |
> |
* @return a string representation of this map |
476 |
|
*/ |
477 |
|
public String toString() { |
489 |
– |
StringBuilder sb = new StringBuilder(); |
490 |
– |
sb.append("{"); |
491 |
– |
|
478 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
479 |
< |
boolean hasNext = i.hasNext(); |
480 |
< |
while (hasNext) { |
479 |
> |
if (! i.hasNext()) |
480 |
> |
return "{}"; |
481 |
> |
|
482 |
> |
StringBuilder sb = new StringBuilder(); |
483 |
> |
sb.append('{'); |
484 |
> |
for (;;) { |
485 |
|
Entry<K,V> e = i.next(); |
486 |
|
K key = e.getKey(); |
487 |
< |
V value = e.getValue(); |
488 |
< |
if (key == this) |
489 |
< |
sb.append("(this Map)"); |
490 |
< |
else |
491 |
< |
sb.append(key); |
492 |
< |
sb.append("="); |
493 |
< |
if (value == this) |
494 |
< |
sb.append("(this Map)"); |
505 |
< |
else |
506 |
< |
sb.append(value); |
507 |
< |
hasNext = i.hasNext(); |
508 |
< |
if (hasNext) |
509 |
< |
sb.append(", "); |
510 |
< |
} |
511 |
< |
|
512 |
< |
sb.append("}"); |
513 |
< |
return sb.toString(); |
487 |
> |
V value = e.getValue(); |
488 |
> |
sb.append(key == this ? "(this Map)" : key); |
489 |
> |
sb.append('='); |
490 |
> |
sb.append(value == this ? "(this Map)" : value); |
491 |
> |
if (! i.hasNext()) |
492 |
> |
return sb.append('}').toString(); |
493 |
> |
sb.append(", "); |
494 |
> |
} |
495 |
|
} |
496 |
|
|
497 |
|
/** |
595 |
|
return oldValue; |
596 |
|
} |
597 |
|
|
598 |
+ |
/** |
599 |
+ |
* Compares the specified object with this entry for equality. |
600 |
+ |
* Returns {@code true} if the given object is also a map entry and |
601 |
+ |
* the two entries represent the same mapping. More formally, two |
602 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
603 |
+ |
* if<pre> |
604 |
+ |
* (e1.getKey()==null ? |
605 |
+ |
* e2.getKey()==null : |
606 |
+ |
* e1.getKey().equals(e2.getKey())) |
607 |
+ |
* && |
608 |
+ |
* (e1.getValue()==null ? |
609 |
+ |
* e2.getValue()==null : |
610 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
611 |
+ |
* This ensures that the {@code equals} method works properly across |
612 |
+ |
* different implementations of the {@code Map.Entry} interface. |
613 |
+ |
* |
614 |
+ |
* @param o object to be compared for equality with this map entry |
615 |
+ |
* @return {@code true} if the specified object is equal to this map |
616 |
+ |
* entry |
617 |
+ |
* @see #hashCode |
618 |
+ |
*/ |
619 |
|
public boolean equals(Object o) { |
620 |
|
if (!(o instanceof Map.Entry)) |
621 |
|
return false; |
623 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
624 |
|
} |
625 |
|
|
626 |
+ |
/** |
627 |
+ |
* Returns the hash code value for this map entry. The hash code |
628 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
629 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
630 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
631 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
632 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
633 |
+ |
* {@code e1} and {@code e2}, as required by the general |
634 |
+ |
* contract of {@link Object#hashCode}. |
635 |
+ |
* |
636 |
+ |
* @return the hash code value for this map entry |
637 |
+ |
* @see #equals |
638 |
+ |
*/ |
639 |
|
public int hashCode() { |
640 |
|
return (key == null ? 0 : key.hashCode()) ^ |
641 |
|
(value == null ? 0 : value.hashCode()); |
726 |
|
throw new UnsupportedOperationException(); |
727 |
|
} |
728 |
|
|
729 |
+ |
/** |
730 |
+ |
* Compares the specified object with this entry for equality. |
731 |
+ |
* Returns {@code true} if the given object is also a map entry and |
732 |
+ |
* the two entries represent the same mapping. More formally, two |
733 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
734 |
+ |
* if<pre> |
735 |
+ |
* (e1.getKey()==null ? |
736 |
+ |
* e2.getKey()==null : |
737 |
+ |
* e1.getKey().equals(e2.getKey())) |
738 |
+ |
* && |
739 |
+ |
* (e1.getValue()==null ? |
740 |
+ |
* e2.getValue()==null : |
741 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
742 |
+ |
* This ensures that the {@code equals} method works properly across |
743 |
+ |
* different implementations of the {@code Map.Entry} interface. |
744 |
+ |
* |
745 |
+ |
* @param o object to be compared for equality with this map entry |
746 |
+ |
* @return {@code true} if the specified object is equal to this map |
747 |
+ |
* entry |
748 |
+ |
* @see #hashCode |
749 |
+ |
*/ |
750 |
|
public boolean equals(Object o) { |
751 |
|
if (!(o instanceof Map.Entry)) |
752 |
|
return false; |
754 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
755 |
|
} |
756 |
|
|
757 |
+ |
/** |
758 |
+ |
* Returns the hash code value for this map entry. The hash code |
759 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
760 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
761 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
762 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
763 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
764 |
+ |
* {@code e1} and {@code e2}, as required by the general |
765 |
+ |
* contract of {@link Object#hashCode}. |
766 |
+ |
* |
767 |
+ |
* @return the hash code value for this map entry |
768 |
+ |
* @see #equals |
769 |
+ |
*/ |
770 |
|
public int hashCode() { |
771 |
|
return (key == null ? 0 : key.hashCode()) ^ |
772 |
|
(value == null ? 0 : value.hashCode()); |