This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
API Note
This class is not exported. Instances are created by various Map implementations when they need a Map.Entry that isn't connected to the Map.
This class differs from AbstractMap.SimpleImmutableEntry in that it is not serializable and that it is final. This class differs from java.util.KeyValueHolder in that the key and value are nullable.
In principle this class could be a variation on KeyValueHolder. However, making that class selectively support nullable keys and values is quite intricate. Various specifications (such as Map.ofEntries and Map.entry) specify non-nullability of the key and the value. Map.Entry.copyOf also requires non-null keys and values; but it simply passes through KeyValueHolder instances, assuming their keys and values are non-nullable. If a KVH with nullable keys and values were introduced, some way to distinguish it would be necessary. This could be done by introducing a subclass (requiring KVH to be made non-final) or by introducing some kind of "mode" field (potentially increasing the size of every KVH instance, though another field could probably fit into the object's padding in most JVMs.) More critically, a mode field would have to be checked in all the right places to get the right behavior.
A longer range possibility is to selectively relax the restrictions against nulls in Map.entry and Map.Entry.copyOf. This would also require some intricate specification changes and corresponding implementation changes (e.g., the implementations backing Map.of might still need to reject nulls, and so would Map.ofEntries) but allowing a Map.Entry itself to contain nulls seems beneficial in general. If this is done, merging KeyValueHolder and NullableKeyValueHolder should be reconsidered.
Access | Constructor and Description |
---|---|
public | NullableKeyValueHolder(K
the key, may be null k, V the value, may be null v)Constructs a NullableKeyValueHolder. |
public | NullableKeyValueHolder(Map.
the entry, must not be null entryConstructs a NullableKeyValueHolder from a Map.Entry. |
Modifier and Type | Method and Description |
---|---|
public boolean | equals(Object
object to be compared for equality with this map entry o)Overrides java. Implements java. Compares the specified object with this entry for equality. |
public K | Returns: the key, may be nullImplements java. Gets the key from this holder. |
public V | Returns: the value, may be nullImplements java. Gets the value from this holder. |
private int | |
public int | hashCode()
Overrides java. Implements java. Returns the hash code value for this map entry. |
public V | Returns: never returns normallyignored value)Implements java. Throws |
public String | Returns: a String representation of this map entryOverrides java. Returns a String representation of this map entry. |
NullableKeyValueHolder | back to summary |
---|---|
public NullableKeyValueHolder(K k, V v) Constructs a NullableKeyValueHolder.
|
NullableKeyValueHolder | back to summary |
---|---|
public NullableKeyValueHolder(Map. Constructs a NullableKeyValueHolder from a Map.Entry. No need for an idempotent copy at this time. |
equals | back to summary |
---|---|
public boolean equals(Object o) Overrides java. Implements java. Compares the specified object with this entry for equality.
Returns
|
getKey | back to summary |
---|---|
public K getKey() Implements java. Gets the key from this holder.
|
getValue | back to summary |
---|---|
public V getValue() Implements java. Gets the value from this holder.
|
hash | back to summary |
---|---|
private int hash(Object obj) |
hashCode | back to summary |
---|---|
public int hashCode() Overrides java. Implements java. Returns the hash code value for this map entry. The hash code
is
|
setValue | back to summary |
---|---|
public V setValue(V value) Implements java. Throws
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a String representation of this map entry. This
implementation returns the string representation of this
entry's key followed by the equals character (" |