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 stability is linked by default.

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

By: Stéphane Épardaud

no subtypes hierarchy

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

Create a new HashMap with the given initial entries.

Parameters:
  • 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.

Parameters:
  • 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.

Attributes
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.

In general, hash values vary between platforms and between executions of the same program.

Note that when executing on a Java Virtual Machine, the 64-bit Integer value returned by an implementation of hash is truncated to a 32-bit integer value by taking the exclusive disjunction of the 32 lowest-order bits with the 32 highest-order bits, before returning the value to the caller.

Refines Identifiable.hash ultimately refines Object.hash
lastSource Codeshared actual <Key->Item>? last

The last element returned by the iterator, if any, or null if this stream is empty. In the case of an infinite stream, this operation never terminates; furthermore, this default implementation iterates all elements, which might be very expensive.

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>
Methods
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 any instance c of Category, c.contains(element) may be written using the in operator:

element in c

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) {
    print(word.lowercased);
    print(word.uppercased);
});

Has the same effect as the following for loop:

for (word in words) {
    print(word.lowercased);
    print(word.uppercased);
}

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.

For any two non-null objects x and y, x.equals(y) may be written as:

x == y 

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.

Note that an implementation of equals() that always returns false does satisfy the constraints given above, as long as the class does not inherit Identifiable. Therefore, in very rare cases where there is no reasonable definition of value equality for a class, for example, function references (Callable), it is acceptable for equals() to be defined to return false for every argument.

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.

For any instance c of Correspondence, c.get(key) may be written using the item operator:

c[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.

If this is a nonempty stream with type {Element+}, the iterator must produce at least one element.

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

Set the item associated with the given key to the given item. If there is already an item associated with this key, replace the association. Otherwise, create a new association.

For any instance c of KeyedCorrespondenceMutator, c.put(key, item) may be written using the item and assignment operators:

c[key] = item
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: KeyedCorrespondenceMutator<Key,Item>
Methods inherited from: Map<Key,Item>
Methods inherited from: MapMutator<Key,Item>
Methods inherited from: MutableMap<Key,Item>