java.util
Class AbstractMap.SimpleEntry<K,V>

java.lang.Object
  extended by java.util.AbstractMap.SimpleEntry<K,V>
All Implemented Interfaces:
Serializable, Map.Entry<K,V>
Enclosing class:
AbstractMap<K,V>

public static class AbstractMap.SimpleEntry<K,V>
extends Object
implements Map.Entry<K,V>, Serializable

A class which implements Map.Entry. It is shared by HashMap, TreeMap, Hashtable, and Collections. It is not specified by the JDK, but makes life much easier.

Since:
1.6
See Also:
Serialized Form

Constructor Summary
AbstractMap.SimpleEntry(K newKey, V newValue)
          Basic constructor initializes the fields.
AbstractMap.SimpleEntry(Map.Entry<? extends K,? extends V> entry)
           
 
Method Summary
 boolean equals(Object o)
          Compares the specified object with this entry.
 K getKey()
          Get the key corresponding to this entry.
 V getValue()
          Get the value corresponding to this entry.
 int hashCode()
          Returns the hash code of the entry.
 V setValue(V newVal)
          Replaces the value with the specified object.
 String toString()
          This provides a string representation of the entry.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractMap.SimpleEntry

public AbstractMap.SimpleEntry(K newKey,
                               V newValue)
Basic constructor initializes the fields.

Parameters:
newKey - the key
newValue - the value

AbstractMap.SimpleEntry

public AbstractMap.SimpleEntry(Map.Entry<? extends K,? extends V> entry)
Method Detail

equals

public boolean equals(Object o)
Compares the specified object with this entry. Returns true only if the object is a mapping of identical key and value. In other words, this must be:
(o instanceof Map.Entry)
       && (getKey() == null ? ((HashMap) o).getKey() == null
           : getKey().equals(((HashMap) o).getKey()))
       && (getValue() == null ? ((HashMap) o).getValue() == null
           : getValue().equals(((HashMap) o).getValue()))

Specified by:
equals in interface Map.Entry<K,V>
Overrides:
equals in class Object
Parameters:
o - the object to compare
Returns:
true if it is equal
See Also:
Object.hashCode()

getKey

public K getKey()
Get the key corresponding to this entry.

Specified by:
getKey in interface Map.Entry<K,V>
Returns:
the key

getValue

public V getValue()
Get the value corresponding to this entry. If you already called Iterator.remove(), the behavior undefined, but in this case it works.

Specified by:
getValue in interface Map.Entry<K,V>
Returns:
the value

hashCode

public int hashCode()
Returns the hash code of the entry. This is defined as the exclusive-or of the hashcodes of the key and value (using 0 for null). In other words, this must be:
(getKey() == null ? 0 : getKey().hashCode())
       ^ (getValue() == null ? 0 : getValue().hashCode())

Specified by:
hashCode in interface Map.Entry<K,V>
Overrides:
hashCode in class Object
Returns:
the hash code
See Also:
Object.equals(Object), System.identityHashCode(Object)

setValue

public V setValue(V newVal)
Replaces the value with the specified object. This writes through to the map, unless you have already called Iterator.remove(). It may be overridden to restrict a null value.

Specified by:
setValue in interface Map.Entry<K,V>
Parameters:
newVal - the new value to store
Returns:
the old value
Throws:
NullPointerException - if the map forbids null values.
UnsupportedOperationException - if the map doesn't support put().
ClassCastException - if the value is of a type unsupported by the map.
IllegalArgumentException - if something else about this value prevents it being stored in the map.

toString

public String toString()
This provides a string representation of the entry. It is of the form "key=value", where string concatenation is used on key and value.

Overrides:
toString in class Object
Returns:
the string representation
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)