pub struct HashMap<K: Hash + Eq, V, B: BuildHasher = RandomState> { /* private fields */ }
Expand description
A map of keys to values which relies on the keys implementing Hash
.
A custom load factor is not supported at this point, with the default being 4/5.
It is a logic error for keys in a HashMap to be manipulated in a way that changes their hash. Because of this, HashMap’s API prevents mutable access to its keys.
§Time Complexity
For this analysis of time complexity, variables are defined as follows:
n
: The number of items in the HashMap.
Method | Complexity |
---|---|
len | O(1) |
insert | O(1) **, O(n) |
insert_unchecked | O(1) * |
get | O(1) * |
remove | O(1) * |
contains | O(1) * |
reserve | O(n) ***, O(1) |
* In the event of a has collision, these functions will take additional time, while a valid / correct location is found. This additional time is kept at a minimum and hash collisions are unlikely especially with a large capacity.
** If the HashMap doesn’t have enough capacity for the new element, insert
will take O(n)
.
* applies as well.
*** If the HashMap has enough capacity for the additional items already, reserve
is O(1)
.
Implementations§
Source§impl<K: Hash + Eq, V, B: BuildHasher + Default> HashMap<K, V, B>
impl<K: Hash + Eq, V, B: BuildHasher + Default> HashMap<K, V, B>
Source§impl<K: Hash + Eq, V, B: BuildHasher> HashMap<K, V, B>
impl<K: Hash + Eq, V, B: BuildHasher> HashMap<K, V, B>
Sourcepub fn with_hasher(hasher: B) -> HashMap<K, V, B>
pub fn with_hasher(hasher: B) -> HashMap<K, V, B>
Creates a new HashMap with capacity 0 and the provided hasher
.
Sourcepub fn with_cap_and_hasher(cap: usize, hasher: B) -> HashMap<K, V, B>
pub fn with_cap_and_hasher(cap: usize, hasher: B) -> HashMap<K, V, B>
Creates a new HashMap with the provided cap
acity and hasher
.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the length of the HashMap (the number of entries it contains).
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts the provided key
-value
pair into the HashMap, increasing its capacity if
required. If the key was already associated with a value, the previous value is returned.
As with the standard library, the key isn’t changed if it already exists.
Sourcepub unsafe fn insert_unchecked(&mut self, key: K, value: V) -> Option<V>
pub unsafe fn insert_unchecked(&mut self, key: K, value: V) -> Option<V>
Inserts the provided key
-value
pair without checking if the HashMap has enough capacity.
If the key was already associated with a value, the previous value is returned.
As with the standard library, the key isn’t changed if it already exists.
§Safety
It is the responsibility of the caller to ensure that the HashMap has enough capacity to add
the provided entry, using methods like reserve
or
with_cap
.
§Panics
Panics if the HashMap has a capacity of 0, as it isn’t possible to find a bucket associated with the key.
Sourcepub fn get_entry<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_entry<Q>(&self, key: &Q) -> Option<(&K, &V)>
Returns the entry for the provided key
as a key-value pair or None if there is no entry.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value associated with the provided key
or None if the map
contains no values for key
.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value associated with the provided key
or None if the
map contains no values for key
.
Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
Removes the entry associated with key
, returning it if it exists.
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes the entry associated with key
, returning the value if it exists.
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
Returns true if there is a value associated with the provided key
.
Sourcepub fn reserve(&mut self, extra: usize)
pub fn reserve(&mut self, extra: usize)
Increases the capacity of the HashMap to ensure that len + extra
entries will fit without
exceeding the load factor.
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns an iterator over all key-value pairs in the HashMap, as references.
Sourcepub fn into_keys(self) -> IntoKeys<K, V> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V> ⓘ
Consumes self and returns an iterator over all contained keys.
Sourcepub fn keys<'a>(&'a self) -> Keys<'a, K, V> ⓘ
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> ⓘ
Returns an iterator over all keys in the HashMap, as references.
Sourcepub fn into_values(self) -> IntoValues<K, V> ⓘ
pub fn into_values(self) -> IntoValues<K, V> ⓘ
Consumes self and returns an iterator over all contained values.
Sourcepub fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V> ⓘ
pub fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V> ⓘ
Returns an iterator over all values in the HashMap, as mutable references.
Trait Implementations§
Source§impl<K: Hash + Eq, V, B: BuildHasher + Default> Extend<(K, V)> for HashMap<K, V, B>
impl<K: Hash + Eq, V, B: BuildHasher + Default> Extend<(K, V)> for HashMap<K, V, B>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: (K, V))
fn extend_one(&mut self, item: (K, V))
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)