Interface ImmutableMap<K,V>

Type Parameters:
K - the key type of content of this Map
V - 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
A Map interface without the methods that can change it. The constructor of the ImmutableMap needs to be given an initial Map.

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

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    A map entry (key-value pair).
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if this map contains a mapping for the specified key.
    boolean
    Returns true if this map maps one or more keys to the specified value.
    Returns a Set 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.
    get(Object key)
    Returns the value to which the specified key is mapped, or null 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, or defaultValue if this map contains no mapping for the key.
    int
    Force to redefine hashCode for the implementations of immutable collection classes.
    default boolean
    Return whether the internal storage is a (shallow) copy of the original map.
    boolean
    Returns true if this immutable collection contains no elements.
    boolean
    Return whether the internal storage is a wrapped pointer to the original map.
    Returns a Set 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
    Returns the number of elements in this immutable collection.
    Returns a modifiable copy of this immutable map.
    Force to redefine toString.
    Returns a ImmutableCollection 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 than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
      Returns:
      the number of elements in this immutable collection
    • isEmpty

      boolean isEmpty()
      Returns true if this immutable collection contains no elements.
      Returns:
      true if this immutable collection contains no elements
    • containsKey

      boolean containsKey(Object key)
      Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k 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 map
      NullPointerException - if the specified key is null and this map does not permit null keys
    • containsValue

      boolean containsValue(Object value)
      Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map 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 map
      NullPointerException - if the specified value is null and this map does not permit null values
    • get

      V get(Object key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

      More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (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 to null. The containsKey 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 map
      NullPointerException - if the specified key is null and this map does not permit null keys
    • keySet

      ImmutableSet<K> keySet()
      Returns a Set view of the keys contained in this map.
      Returns:
      an immutable set of the keys contained in this map
    • entrySet

      Returns a Set view of the entries contained in this map.
      Returns:
      an immutable set of the entries contained in this map
    • values

      Returns a ImmutableCollection view of the values contained in this map.
      Returns:
      an immutable collection view of the values contained in this map
    • getOrDefault

      default V getOrDefault(Object key, V defaultValue)
      Returns the value to which the specified key is mapped, or defaultValue 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 returned
      defaultValue - 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 map
      NullPointerException - if the specified key is null and this map does not permit null keys
    • forEach

      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. 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 null
      ConcurrentModificationException - if an entry is found to be removed during iteration
    • toMap

      Map<K,V> toMap()
      Returns a modifiable copy of this immutable map.
      Returns:
      a modifiable copy of this immutable map.
    • equals

      boolean equals(Object obj)
      Force to redefine equals for the implementations of immutable collection classes.
      Overrides:
      equals in class Object
      Parameters:
      obj - Object; the object to compare this collection with
      Returns:
      whether the objects are equal
    • hashCode

      int hashCode()
      Force to redefine hashCode for the implementations of immutable collection classes.
      Overrides:
      hashCode in class Object
      Returns:
      the calculated hashCode
    • 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

      static <K, V> ImmutableMap<K,V> of()
      Return an empty ImmutableMap, backed by a LinkedHashMap.
      Type Parameters:
      K - the key type
      V - the value type
      Returns:
      ImmutableMap<K, V>; an empty ImmutableMap
    • of

      static <K, V> ImmutableMap<K,V> of(K k1, V v1)
      Return an ImmutableMap with 1 entry, backed by a LinkedHashMap.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      Returns:
      ImmutableMap<K, V>; an ImmutableMap with 1 entry, backed by a LinkedHashMap
    • of

      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.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      k2 - K; key 2
      v2 - V; value 2
      Returns:
      ImmutableMap<K, V>; an ImmutableMap with 2 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)
      Return an ImmutableMap with 3 entries, backed by a LinkedHashMap.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      k2 - K; key 2
      v2 - V; value 2
      k3 - K; key 3
      v3 - V; value 3
      Returns:
      ImmutableMap<K, V>; an ImmutableMap with 3 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)
      Return an ImmutableMap with 4 entries, backed by a LinkedHashMap.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      k2 - K; key 2
      v2 - V; value 2
      k3 - K; key 3
      v3 - V; value 3
      k4 - K; key 4
      v4 - V; value 4
      Returns:
      ImmutableMap<K, V>; an ImmutableMap with 4 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)
      Return an ImmutableMap with 5 entries, backed by a LinkedHashMap.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      k2 - K; key 2
      v2 - V; value 2
      k3 - K; key 3
      v3 - V; value 3
      k4 - K; key 4
      v4 - V; value 4
      k5 - K; key 5
      v5 - 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 type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      k2 - K; key 2
      v2 - V; value 2
      k3 - K; key 3
      v3 - V; value 3
      k4 - K; key 4
      v4 - V; value 4
      k5 - K; key 5
      v5 - V; value 5
      k6 - K; key 6
      v6 - 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 type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      k2 - K; key 2
      v2 - V; value 2
      k3 - K; key 3
      v3 - V; value 3
      k4 - K; key 4
      v4 - V; value 4
      k5 - K; key 5
      v5 - V; value 5
      k6 - K; key 6
      v6 - V; value 6
      k7 - K; key 7
      v7 - 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 type
      V - the value type
      Parameters:
      k1 - K; key 1
      v1 - V; value 1
      k2 - K; key 2
      v2 - V; value 2
      k3 - K; key 3
      v3 - V; value 3
      k4 - K; key 4
      v4 - V; value 4
      k5 - K; key 5
      v5 - V; value 5
      k6 - K; key 6
      v6 - V; value 6
      k7 - K; key 7
      v7 - V; value 7
      k8 - K; key 8
      v8 - V; value 8
      Returns:
      ImmutableMap<K, V>; an ImmutableMap with 8 entries, backed by a LinkedHashMap
    • toString

      String toString()
      Force to redefine toString.
      Overrides:
      toString in class Object
      Returns:
      String; a description of this immutable map