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-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)
    Returns true if this map contains a mapping for the specified key.
    boolean containsValue​(Object value)
    Returns true if this map maps one or more keys to the specified value.
    ImmutableSet<ImmutableMap.ImmutableEntry<K,​V>> entrySet()
    Returns a Set 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, 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 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()
    Returns true 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 a Set 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 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 list.
      Returns:
      a modifiable copy of this immutable list.
    • 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
    • toString

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