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
public interface ImmutableMap<K,V> extends Serializable
Copyright (c) 2016-2020 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, Wouter Schakel
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
ImmutableMap.ImmutableEntry<K,V>
A map entry (key-value pair). -
Method Summary
Modifier and Type Method Description boolean
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.ImmutableSet<ImmutableMap.ImmutableEntry<K,V>>
entrySet()
Returns aSet
view of the entries contained in this map.boolean
equals(Object obj)
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.V
get(Object key)
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.ImmutableSet<K>
keySet()
Returns aSet
view of the keys contained in this map.int
size()
Returns the number of elements in this immutable collection.Map<K,V>
toMap()
Returns a modifiable copy of this immutable list.String
toString()
Force to redefine toString.ImmutableCollection<V>
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 list.- Returns:
- a modifiable copy of this immutable list.
-
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
-
toString
String toString()Force to redefine toString.
-