Interface ImmutableMap<K,V>
- Type Parameters:
K
- the key type of content of this MapV
- the value type of content of this Map
- All Superinterfaces:
Serializable
- All Known Subinterfaces:
ImmutableNavigableMap<K,
,V> ImmutableSortedMap<K,
V>
- All Known Implementing Classes:
ImmutableAbstractMap
,ImmutableHashMap
,ImmutableLinkedHashMap
,ImmutableTreeMap
Copyright (c) 2016-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.
- Author:
- Alexander Verbraeck, Peter Knoppers
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic class
A map entry (key-value pair). -
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey
(Object key) Returnstrue
if this map contains a mapping for the specified key.boolean
containsValue
(Object value) Returnstrue
if this map maps one or more keys to the specified value.entrySet()
Returns aSet
view of the entries contained in this map.boolean
Force to redefine equals for the implementations of immutable collection classes.default void
forEach
(BiConsumer<? super K, ? super V> action) Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.default V
getOrDefault
(Object key, V defaultValue) Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.int
hashCode()
Force to redefine hashCode for the implementations of immutable collection classes.default boolean
isCopy()
Return whether the internal storage is a (shallow) copy of the original map.boolean
isEmpty()
Returnstrue
if this immutable collection contains no elements.boolean
isWrap()
Return whether the internal storage is a wrapped pointer to the original map.keySet()
Returns aSet
view of the keys contained in this map.static <K,
V> ImmutableMap<K, V> of()
Return an empty ImmutableMap, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1) Return an ImmutableMap with 1 entry, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1, K k2, V v2) Return an ImmutableMap with 2 entries, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3) Return an ImmutableMap with 3 entries, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) Return an ImmutableMap with 4 entries, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) Return an ImmutableMap with 5 entries, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Return an ImmutableMap with 6 entries, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Return an ImmutableMap with 7 entries, backed by a LinkedHashMap.static <K,
V> ImmutableMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Return an ImmutableMap with 8 entries, backed by a LinkedHashMap.int
size()
Returns the number of elements in this immutable collection.toMap()
Returns a modifiable copy of this immutable map.toString()
Force to redefine toString.values()
Returns aImmutableCollection
view of the values contained in this map.
-
Method Details
-
size
int size()Returns the number of elements in this immutable collection. If this immutable collection contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.- Returns:
- the number of elements in this immutable collection
-
isEmpty
boolean isEmpty()Returnstrue
if this immutable collection contains no elements.- Returns:
true
if this immutable collection contains no elements
-
containsKey
Returnstrue
if this map contains a mapping for the specified key. More formally, returnstrue
if and only if this map contains a mapping for a keyk
such that(key==null ? k==null : key.equals(k))
. (There can be at most one such mapping.)- Parameters:
key
- Object; key whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified key- Throws:
ClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- if the specified key is null and this map does not permit null keys
-
containsValue
Returnstrue
if this map maps one or more keys to the specified value. More formally, returnstrue
if and only if this map contains at least one mapping to a valuev
such that(value==null ? v==null : value.equals(v))
. This operation will probably require time linear in the map size for most implementations of theMap
interface.- Parameters:
value
- Object; value whose presence in this map is to be tested- Returns:
true
if this map maps one or more keys to the specified value- Throws:
ClassCastException
- if the value is of an inappropriate type for this mapNullPointerException
- if the specified value is null and this map does not permit null values
-
get
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.More formally, if this map contains a mapping from a key
k
to a valuev
such that(key==null ? k==null : key.equals(k))
, then this method returnsv
; otherwise it returnsnull
. (There can be at most one such mapping.)If this map permits null values, then a return value of
null
does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull
. ThecontainsKey
operation may be used to distinguish these two cases.- Parameters:
key
- Object; the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
null
if this map contains no mapping for the key - Throws:
ClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- if the specified key is null and this map does not permit null keys
-
keySet
ImmutableSet<K> keySet()Returns aSet
view of the keys contained in this map.- Returns:
- an immutable set of the keys contained in this map
-
entrySet
ImmutableSet<ImmutableMap.ImmutableEntry<K,V>> entrySet()Returns aSet
view of the entries contained in this map.- Returns:
- an immutable set of the entries contained in this map
-
values
ImmutableCollection<V> values()Returns aImmutableCollection
view of the values contained in this map.- Returns:
- an immutable collection view of the values contained in this map
-
getOrDefault
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key. The default implementation makes no guarantees about synchronization or atomicity properties of this method. Any implementation providing atomicity guarantees must override this method and document its concurrency properties.- Parameters:
key
- Object; the key whose associated value is to be returneddefaultValue
- V; the default mapping of the key- Returns:
- the value to which the specified key is mapped, or
defaultValue
if this map contains no mapping for the key - Throws:
ClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- if the specified key is null and this map does not permit null keys
-
forEach
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of entry set iteration (if an iteration order is specified.) Exceptions thrown by the action are relayed to the caller. The default implementation makes no guarantees about synchronization or atomicity properties of this method. Any implementation providing atomicity guarantees must override this method and document its concurrency properties.- Parameters:
action
- BiConsumer<? super K,? super V>; The action to be performed for each entry- Throws:
NullPointerException
- if the specified action is nullConcurrentModificationException
- if an entry is found to be removed during iteration
-
toMap
Returns a modifiable copy of this immutable map.- Returns:
- a modifiable copy of this immutable map.
-
equals
Force to redefine equals for the implementations of immutable collection classes. -
hashCode
int hashCode()Force to redefine hashCode for the implementations of immutable collection classes. -
isWrap
boolean isWrap()Return whether the internal storage is a wrapped pointer to the original map. If true, this means that anyone holding a pointer to this data structure can still change it. The users of the ImmutableMap itself can, however, not make any changes.- Returns:
- boolean; whether the internal storage is a wrapped pointer to the original map
-
isCopy
default boolean isCopy()Return whether the internal storage is a (shallow) copy of the original map. If true, this means that anyone holding a pointer to the original of the data structure can not change it anymore. Nor can the users of the ImmutableMap itself make any changes.- Returns:
- boolean; whether the internal storage is a safe copy of the original map
-
of
Return an empty ImmutableMap, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- ImmutableMap<K, V>; an empty ImmutableMap
-
of
Return an ImmutableMap with 1 entry, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 1 entry, backed by a LinkedHashMap
-
of
Return an ImmutableMap with 2 entries, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1k2
- K; key 2v2
- V; value 2- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 2 entries, backed by a LinkedHashMap
-
of
Return an ImmutableMap with 3 entries, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1k2
- K; key 2v2
- V; value 2k3
- K; key 3v3
- V; value 3- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 3 entries, backed by a LinkedHashMap
-
of
Return an ImmutableMap with 4 entries, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1k2
- K; key 2v2
- V; value 2k3
- K; key 3v3
- V; value 3k4
- K; key 4v4
- V; value 4- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 4 entries, backed by a LinkedHashMap
-
of
Return an ImmutableMap with 5 entries, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1k2
- K; key 2v2
- V; value 2k3
- K; key 3v3
- V; value 3k4
- K; key 4v4
- V; value 4k5
- K; key 5v5
- V; value 5- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 5 entries, backed by a LinkedHashMap
-
of
static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Return an ImmutableMap with 6 entries, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1k2
- K; key 2v2
- V; value 2k3
- K; key 3v3
- V; value 3k4
- K; key 4v4
- V; value 4k5
- K; key 5v5
- V; value 5k6
- K; key 6v6
- V; value 6- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 6 entries, backed by a LinkedHashMap
-
of
static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Return an ImmutableMap with 7 entries, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1k2
- K; key 2v2
- V; value 2k3
- K; key 3v3
- V; value 3k4
- K; key 4v4
- V; value 4k5
- K; key 5v5
- V; value 5k6
- K; key 6v6
- V; value 6k7
- K; key 7v7
- V; value 7- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 7 entries, backed by a LinkedHashMap
-
of
static <K,V> ImmutableMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Return an ImmutableMap with 8 entries, backed by a LinkedHashMap.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
- K; key 1v1
- V; value 1k2
- K; key 2v2
- V; value 2k3
- K; key 3v3
- V; value 3k4
- K; key 4v4
- V; value 4k5
- K; key 5v5
- V; value 5k6
- K; key 6v6
- V; value 6k7
- K; key 7v7
- V; value 7k8
- K; key 8v8
- V; value 8- Returns:
- ImmutableMap<K, V>; an ImmutableMap with 8 entries, backed by a LinkedHashMap
-
toString
String toString()Force to redefine toString.
-