HashMap<K, V> class
A custom implementation of a hash map that implements the Map<K, V>
interface.
This map uses open addressing with chaining to resolve hash collisions.
Each bucket holds a list of _HashMapEntry<K, V>
objects, allowing multiple entries
to exist in the same bucket if they hash to the same index.
It supports automatic resizing when the load factor exceeds 0.75
, ensuring
performance remains optimal as the number of entries grows.
This is useful for educational purposes or when custom behavior is required
beyond what dart:collection
offers.
βοΈ Internal Design:
- Backed by an array of lists:
List<List<_HashMapEntry<K, V>>?> _buckets
- On collision: new entries are appended to the appropriate list
- Resize logic doubles capacity when load factor exceeds the threshold
π¦ Example Usage:
final map = HashMap<String, int>();
map['apple'] = 3;
map['banana'] = 5;
print(map['apple']); // 3
print(map.containsKey('banana')); // true
print(map.length); // 2
Constructors
- HashMap()
-
A custom implementation of a hash map that implements the
Map<K, V>
interface. -
HashMap.from(Map<
K, V> map) -
Creates a hash map from an existing map.
factory
Properties
-
entries
β Iterable<
MapEntry< K, V> > -
The map entries of this Map.
no setteroverride
- hashCode β int
-
The hash code for this object.
no setterinherited
- isEmpty β bool
-
Whether there is no key/value pair in the map.
no setteroverride
- isNotEmpty β bool
-
Whether there is at least one key/value pair in the map.
no setteroverride
-
keys
β Iterable<
K> -
The keys of this Map.
no setteroverride
- length β int
-
The number of key/value pairs in the map.
no setteroverride
- runtimeType β Type
-
A representation of the runtime type of the object.
no setterinherited
-
values
β Iterable<
V> -
The values of this Map.
no setteroverride
Methods
-
add(
K key, V item) β void -
Available on Map<
Add itemK, V> , provided by the MapExtensions extension -
addAll(
Map< K, V> other) β void -
Adds all key/value pairs of
other
to this map.override -
addEntries(
Iterable< MapEntry< newEntries) β voidK, V> > -
Adds all key/value pairs of
newEntries
to this map.override -
asJson(
{String delimiter = '_'}) β Map< String, dynamic> -
Available on Map<
Converts a Map to JSON with formatted keys (K, V> , provided by the MapExtensions extension_
,-
, orcamelCase
). -
cast<
RK, RV> () β Map< RK, RV> -
Provides a view of this map as having
RK
keys andRV
instances, if necessary.override -
clear(
) β void -
Removes all entries from the map.
override
-
computeIfAbsent(
K key, V ifAbsent(K key)) β V -
Available on Map<
Computes a value for the given key if it is not already present in the map.K, V> , provided by the MapExtensions extension -
containsKey(
Object? key) β bool -
Whether this map contains the given
key
.override -
containsValue(
Object? value) β bool -
Whether this map contains the given
value
.override -
forEach(
void action(K key, V value)) β void -
Applies
action
to each key/value pair of the map.override -
get(
K key) β V? -
Available on Map<
Get value by keyK, V> , provided by the MapExtensions extension -
map<
K2, V2> (MapEntry< K2, V2> convert(K key, V value)) β Map<K2, V2> -
Returns a new map where all entries of this map are transformed by
the given
convert
function.override -
mapKeys<
K2> (K2 convert(K key)) β Map< K2, V> -
Available on Map<
Applies a transformation function to the keys of the Map.K, V> , provided by the MapExtensions extension -
merge(
Map< K, V> other) β Map<K, V> -
Available on Map<
Merges another Map into the current Map, overriding duplicate keys.K, V> , provided by the MapExtensions extension -
noSuchMethod(
Invocation invocation) β dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
put(
K key, V value) β void -
Available on Map<
Add itemK, V> , provided by the MapExtensions extension -
putIfAbsent(
K key, V ifAbsent()) β V -
Look up the value of
key
, or add a new entry if it isn't there.override -
remove(
Object? key) β V? -
Removes
key
and its associated value, if present, from the map.override -
removeWhere(
bool test(K key, V value)) β void -
Removes all entries of this map that satisfy the given
test
.override -
toString(
) β String -
A string representation of this object.
override
-
update(
K key, V update(V value), {V ifAbsent()?}) β V -
Updates the value for the provided
key
.override -
updateAll(
V update(K key, V value)) β void -
Updates all values.
override
Operators
-
operator ==(
Object other) β bool -
The equality operator.
inherited
-
operator [](
Object? key) β V? -
The value for the given
key
, ornull
ifkey
is not in the map.override -
operator []=(
K key, V value) β void -
Associates the
key
with the givenvalue
.override