Top Description Fields Constructors Methods
jdk.internal.util

public final Class NullableKeyValueHolder<K, V>

extends Object
implements Map.Entry<K, V>
Class Inheritance
All Implemented Interfaces
java.util.Map.Entry
Annotations
@ValueBased
Type Parameters
<K>
the key type
<V>
the value type
Imports
java.util.Map, .Objects

An immutable container for a key and a value, both of which are nullable.

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.

Field Summary

Modifier and TypeField and Description
pack-priv final K
pack-priv final V

Constructor Summary

AccessConstructor and Description
public
NullableKeyValueHolder(K
the key, may be null
k
,
V
the value, may be null
v
)

Constructs a NullableKeyValueHolder.

public
NullableKeyValueHolder(Map.Entry<K, V>
the entry, must not be null
entry
)

Constructs a NullableKeyValueHolder from a Map.Entry.

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
object to be compared for equality with this map entry
o
)

Overrides java.lang.Object.equals.

Implements java.util.Map.Entry.equals.

Compares the specified object with this entry for equality.

public K

Returns:

the key, may be null
getKey
()

Implements java.util.Map.Entry.getKey.

Gets the key from this holder.

public V

Returns:

the value, may be null
getValue
()

Implements java.util.Map.Entry.getValue.

Gets the value from this holder.

private int
hash(Object obj)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.Entry.hashCode.

Returns the hash code value for this map entry.

public V

Returns:

never returns normally
setValue
(V
ignored
value
)

Implements java.util.Map.Entry.setValue.

Throws UnsupportedOperationException.

public String

Returns:

a String representation of this map entry
toString
()

Overrides java.lang.Object.toString.

Returns a String representation of this map entry.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

keyback to summary
pack-priv final K key
valueback to summary
pack-priv final V value

Constructor Detail

NullableKeyValueHolderback to summary
public NullableKeyValueHolder(K k, V v)

Constructs a NullableKeyValueHolder.

Parameters
k:K

the key, may be null

v:V

the value, may be null

NullableKeyValueHolderback to summary
public NullableKeyValueHolder(Map.Entry<K, V> entry)

Constructs a NullableKeyValueHolder from a Map.Entry. No need for an idempotent copy at this time.

Parameters
entry:Map.Entry<K, V>

the entry, must not be null

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Implements java.util.Map.Entry.equals.

Compares the specified object with this entry for equality. Returns true if the given object is also a map entry and the two entries' keys and values are equal.

Parameters
o:Object

Doc from java.util.Map.Entry.equals.

object to be compared for equality with this map entry

Returns:boolean

Doc from java.util.Map.Entry.equals.

true if the specified object is equal to this map entry

Annotations
@Override
getKeyback to summary
public K getKey()

Implements java.util.Map.Entry.getKey.

Gets the key from this holder.

Returns:K

the key, may be null

Annotations
@Override
getValueback to summary
public V getValue()

Implements java.util.Map.Entry.getValue.

Gets the value from this holder.

Returns:V

the value, may be null

Annotations
@Override
hashback to summary
private int hash(Object obj)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.Entry.hashCode.

Returns the hash code value for this map entry. The hash code is key.hashCode() ^ value.hashCode().

Returns:int

Doc from java.util.Map.Entry.hashCode.

the hash code value for this map entry

Annotations
@Override
setValueback to summary
public V setValue(V value)

Implements java.util.Map.Entry.setValue.

Throws UnsupportedOperationException.

Parameters
value:V

ignored

Returns:V

never returns normally

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a String representation of this map entry. This implementation returns the string representation of this entry's key followed by the equals character ("=") followed by the string representation of this entry's value.

Returns:String

a String representation of this map entry

Annotations
@Override