Java Reference
In-Depth Information
A
Map
allows for at most one
null
value as its key and multiple
null
values as its values. However, an
implementation class may restrict
null
as a value in a
Map
.
The methods in the
Map
interface may be classified in the following four categories depending on the operations
they perform:
•
Methods for basic operations
•
Methods for bulk operations
•
Methods for view operations
•
The methods in the basic operations category let you perform basic operations on a
Map
, for example, putting an
entry into a
Map
, getting the value for a specified key, getting the number of entries, removing an entry, checking if the
Map
is empty, etc. Examples of methods in this category are as follows:
Methods for comparison operations
int size()
•
boolean isEmpty()
•
boolean containsKey (Object key)
•
boolean containsValue (Object value)
•
V get(Object key)
•
V getOrDefault(Object key, V defaultValue)
•
V put(K key, V value)
•
V putIfAbsent(K key, V value)
•
V remove (Object key)
•
boolean remove(Object key, Object value)
•
boolean replace(K key, V oldValue, V newValue)
The methods in the bulk operations category let you perform bulk operations on a
Map
such as copying entries to
a
Map
from another
Map
and removing all entries from the
Map
. Examples of methods in this category are as follows:
•
void clear()
•
void putAll (Map<? extends K, ? extends V> t)
•
void replaceAll(BiFunction<? super K,? super V,? extends V> function)
The view operations category contains three methods. Each returns a different view of a
Map
. You can view all
keys in a
Map
as a
Set
, all values as a
Collection
, and all
<key, value>
pairs as a
Set
. Note that all keys and all
<key,
value>
pairs are always unique in a
Map
and that is the reason why you get their
Set
views. Since a
Map
may contain
duplicate values, you get a
Collection
view of its values. Examples of methods in this category are as follows:
•
Set<K> keySet()
•
Collection<V> values()
•
Set<Map. Entry<K, V>>entrySet()
•