java.util

Class AbstractMap.SimpleEntry<K,V>

public static class AbstractMap.SimpleEntry<K,V> extends Object implements 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

Constructor Summary
SimpleEntry(K newKey, V newValue)
Basic constructor initializes the fields.
SimpleEntry(Entry<? extends K,? extends V> entry)
Method Summary
booleanequals(Object o)
Compares the specified object with this entry.
KgetKey()
Get the key corresponding to this entry.
VgetValue()
Get the value corresponding to this entry.
inthashCode()
Returns the hash code of the entry.
VsetValue(V newVal)
Replaces the value with the specified object.
StringtoString()
This provides a string representation of the entry.

Constructor Detail

SimpleEntry

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

Parameters: newKey the key newValue the value

SimpleEntry

public SimpleEntry(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()))

Parameters: o the object to compare

Returns: true if it is equal

getKey

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

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.

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())

Returns: the hash code

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.

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.

Returns: the string representation