A MutableMap implemented as a hash map stored in an Array of singly linked lists of Entrys. Each entry is assigned an index in the array according to the hash code of its key. The hash code of a key is defined by Object.hash.

The stability of a HashMap controls its iteration order:

  • A linked map has a stable and meaningful order of iteration. The entries of the map form a linked list, where new entries are added to the end of the linked list. Iteration of the map follows this linked list, from least recently added elements to most recently added elements.
  • An unlinked map has an unstable iteration order that may change when the map is modified. The order itself is not meaningful to a client.

The management of the backing array is controlled by the given hashtable.

By: Stéphane Épardaud

no subtypes hierarchy

HashMapSource Codeshared HashMap(Stability stability = ..., Hashtable hashtable = ..., {<Key->Item>*} entries = ...)

Create a new HashMap with the given initial entries.

  • stability = linked

    Determines whether this is a linked hash map with a stable iteration order, defaulting to linked (stable).

  • hashtable = Hashtable()

    Performance-related settings for the backing array.

  • entries = {}

    The initial entries in the map, defaulting to no initial entries.

copySource Codeshared copy(HashMap<Key,Item> hashMap, Stability stability = ..., Hashtable hashtable = ...)

Create a new HashMap with the same initial entries as the given hashMap.

  • hashMap

    The HashMap to copy.

  • stability = hashMap.stability

    Determines whether this is a linked hash map with a stable iteration order, defaulting to the stability of the copied HashMap.

  • hashtable = Hashtable()

    Performance-related settings for the backing array.

emptySource Codeshared actual Boolean empty

Determines if the stream is empty, that is to say, if the iterator returns no elements.

Refines Collection.empty ultimately refines Iterable.empty
firstSource Codeshared actual <Key->Item>? first

The first element returned by the iterator, if any, or null if this stream is empty. For a stream with an unstable iteration order, a different value might be produced each time first is evaluated.

hashSource Codeshared actual Integer hash

The hash value of the value, which allows the value to be an element of a hash-based set or key of a hash-based map. Implementations must respect the constraint that:

  • if x==y then x.hash==y.hash.

Therefore, a class which refines equals must also refine hash.

Because the Integer type is platform-dependent a compiler for a given platform is permitted to further manipulate the calculated hash for an object, and the resulting hash may differ between platforms.

Refines Identifiable.hash ultimately refines Object.hash
sizeSource Codeshared actual Integer size

The number of elements returned by the iterator of this stream, if the iterator terminates. In the case of an infinite stream, this operation never terminates.

Inherited Attributes
Attributes inherited from: Object
Attributes inherited from: Collection<Element>
Attributes inherited from: Correspondence<Key,Item>
Attributes inherited from: Iterable<Element,Absent>
Attributes inherited from: Map<Key,Item>
clearSource Codeshared actual void clear()

Remove every entry from this map, leaving an empty map with no entries.

cloneSource Codeshared actual HashMap<Key,Item> clone()

A shallow copy of this collection, that is, a collection with identical elements which does not change if this collection changes. If this collection is immutable, it is acceptable to return a reference to this collection. If this collection is mutable, a newly instantiated collection must be returned.

Refines MutableMap.clone ultimately refines Collection.clone
containsSource Codeshared actual Boolean contains(Object entry)

Returns true if the given value belongs to this Category, that is, if it is an element of this Category, or false otherwise.

For most Categorys the following relationship is satisfied by every pair of elements x and y:

  • if x==y, then x in category == y in category

However, it is possible to form a useful Category consistent with some other equivalence relation, for example ===. Therefore implementations of contains() which do not satisfy this relationship are tolerated.

Refines Map.contains ultimately refines Category.contains
countSource Codeshared actual Integer count(Boolean selecting(Key->Item element))

Produces the number of elements in this stream that satisfy the given predicate function (selecting). For an infinite stream, this method never terminates.

definesSource Codeshared actual Boolean defines(Object key)

Determines if there is a value defined for the given key.

Refines Map.defines ultimately refines Correspondence.defines
eachSource Codeshared actual void each(void step(Key->Item element))

Call the given function (step) for each element of this stream, passing the elements in the order they occur in this stream.

For example:

words.each((word) {

Has the same effect as the following for loop:

for (word in words) {

For certain streams this method is highly efficient, surpassing the performance of for loops on the JVM. Thus, each() is sometimes preferred in highly performance-critical low-level code.

equalsSource Codeshared actual Boolean equals(Object that)

Determine if two values are equal. Implementations should respect the constraints that:

  • if x===y then x==y (reflexivity),
  • if x==y then y==x (symmetry),
  • if x==y and y==z then x==z (transitivity).

Furthermore it is recommended that implementations ensure that if x==y then x and y have the same concrete class.

A class which explicitly refines equals() is said to support value equality, and the equality operator == is considered much more meaningful for such classes than for a class which simply inherits the default implementation of identity equality from Identifiable.

Refines Identifiable.equals ultimately refines Object.equals
getSource Codeshared actual Item? get(Object key)

Returns the value defined for the given key, or null if there is no value defined for the given key.

Refines Map.get ultimately refines Correspondence.get
getOrDefaultSource Codeshared actual Item|Default getOrDefault<Default>(Object key, Default default)

Returns the item of the entry with the given key, or the given default if there is no entry with the given key in this map.

For maps with non-null items, the expression:

map.getOrDefault(key, def)

is equivalent to this common idiom:

map[key] else def

However, when the map has null items, getOrDefault() will preserve them.

Note that high-quality implementations of Map should refine this default implementation.

iteratorSource Codeshared actual Iterator<Key->Item> iterator()

An iterator for the elements belonging to this stream.

putSource Codeshared actual Item? put(Key key, Item item)

Add an entry to this map, overwriting any existing entry for the given key, and returning the previous value associated with the given key, if any, or null if no existing entry was overwritten.

Refines MutableMap.put ultimately refines MapMutator.put
putAllSource Codeshared actual void putAll({<Key->Item>*} entries)

Add the given entries to this map, overwriting any existing entries with the same keys.

removeSource Codeshared actual Item? remove(Key key)

Remove the entry associated with the given key, if any, from this map, returning the value no longer associated with the given key, if any, or null if there was no entry associated with the given key.

Refines MutableMap.remove ultimately refines MapMutator.remove
removeEntrySource Codeshared actual Boolean removeEntry(Key key, Item&Object item)

Remove the entry associated with the given key, if any, only if its item is equal to the given item. Return true if an entry was removed, or false otherwise.

replaceEntrySource Codeshared actual Boolean replaceEntry(Key key, Item&Object item, Item newItem)

Modify the entry associated with the given key, if any, setting its item to the given newItem, only if the its item is currently equal to the given item. Return true if the item was replaced,or false otherwise.

Inherited Methods
Methods inherited from: Object
Methods inherited from: Category<Element>
Methods inherited from: Collection<Element>
Methods inherited from: Correspondence<Key,Item>
Methods inherited from: Iterable<Element,Absent>
Methods inherited from: Map<Key,Item>
Methods inherited from: MapMutator<Key,Item>
Methods inherited from: MutableMap<Key,Item>