pub struct LinkedHashMap<K, V, S = RandomState> { /* private fields */ }Expand description
A linked hash map.
Implementations§
Source§impl<K, V> LinkedHashMap<K, V>
impl<K, V> LinkedHashMap<K, V>
Sourcepub fn new() -> LinkedHashMap<K, V>
pub fn new() -> LinkedHashMap<K, V>
Creates a linked hash map.
Sourcepub fn with_capacity(capacity: usize) -> LinkedHashMap<K, V>
pub fn with_capacity(capacity: usize) -> LinkedHashMap<K, V>
Creates an empty linked hash map with the given initial capacity.
Source§impl<K, V, S> LinkedHashMap<K, V, S>
impl<K, V, S> LinkedHashMap<K, V, S>
Sourcepub fn with_hasher(hash_builder: S) -> LinkedHashMap<K, V, S>
pub fn with_hasher(hash_builder: S) -> LinkedHashMap<K, V, S>
Creates an empty linked hash map with the given initial hash builder.
Sourcepub fn with_capacity_and_hasher(
capacity: usize,
hash_builder: S,
) -> LinkedHashMap<K, V, S>
pub fn with_capacity_and_hasher( capacity: usize, hash_builder: S, ) -> LinkedHashMap<K, V, S>
Creates an empty linked hash map with the given initial capacity and hash builder.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted into the map. The
map may reserve more space to avoid frequent allocations.
§Panics
Panics if the new allocation size overflows usize.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
Sourcepub fn entry(&mut self, k: K) -> Entry<'_, K, V, S>
pub fn entry(&mut self, k: K) -> Entry<'_, K, V, S>
Gets the given key’s corresponding entry in the map for in-place manipulation.
§Examples
use linked_hash_map::LinkedHashMap;
let mut letters = LinkedHashMap::new();
for ch in "a short treatise on fungi".chars() {
let counter = letters.entry(ch).or_insert(0);
*counter += 1;
}
assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);Sourcepub fn entries(&mut self) -> Entries<'_, K, V, S> ⓘ
pub fn entries(&mut self) -> Entries<'_, K, V, S> ⓘ
Returns an iterator visiting all entries in insertion order.
Iterator element type is OccupiedEntry<K, V, S>. Allows for removal
as well as replacing the entry.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
{
let mut iter = map.entries();
let mut entry = iter.next().unwrap();
assert_eq!(&"a", entry.key());
*entry.get_mut() = 17;
}
assert_eq!(&17, map.get(&"a").unwrap());Sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
Inserts a key-value pair into the map. If the key already existed, the old value is returned.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
assert_eq!(map[&1], "a");
assert_eq!(map[&2], "b");Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Checks if the map contains the given key.
Sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns the value corresponding to the key in the map.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(2, "c");
map.insert(3, "d");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), Some(&"c"));Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
Returns the mutable reference corresponding to the key in the map.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
*map.get_mut(&1).unwrap() = "c";
assert_eq!(map.get(&1), Some(&"c"));Sourcepub fn get_refresh<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_refresh<Q>(&mut self, k: &Q) -> Option<&mut V>
Returns the value corresponding to the key in the map.
If value is found, it is moved to the end of the list. This operation can be used in implemenation of LRU cache.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "d");
assert_eq!(map.get_refresh(&2), Some(&mut "b"));
assert_eq!((&2, &"b"), map.iter().rev().next().unwrap());Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Removes and returns the value corresponding to the key from the map.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(2, "a");
assert_eq!(map.remove(&1), None);
assert_eq!(map.remove(&2), Some("a"));
assert_eq!(map.remove(&2), None);
assert_eq!(map.len(), 0);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the maximum number of key-value pairs the map can hold without reallocating.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map: LinkedHashMap<i32, &str> = LinkedHashMap::new();
let capacity = map.capacity();Sourcepub fn pop_front(&mut self) -> Option<(K, V)>
pub fn pop_front(&mut self) -> Option<(K, V)>
Removes the first entry.
Can be used in implementation of LRU cache.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_front();
assert_eq!(map.get(&1), None);
assert_eq!(map.get(&2), Some(&20));Sourcepub fn front(&self) -> Option<(&K, &V)>
pub fn front(&self) -> Option<(&K, &V)>
Gets the first entry.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.front(), Some((&1, &10)));Sourcepub fn pop_back(&mut self) -> Option<(K, V)>
pub fn pop_back(&mut self) -> Option<(K, V)>
Removes the last entry.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_back();
assert_eq!(map.get(&1), Some(&10));
assert_eq!(map.get(&2), None);Sourcepub fn back(&self) -> Option<(&K, &V)>
pub fn back(&self) -> Option<(&K, &V)>
Gets the last entry.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.back(), Some((&2, &20)));Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns a double-ended iterator visiting all key-value pairs in order of insertion.
Iterator element type is (&'a K, &'a V)
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
let mut iter = map.iter();
assert_eq!((&"a", &10), iter.next().unwrap());
assert_eq!((&"c", &30), iter.next().unwrap());
assert_eq!((&"b", &20), iter.next().unwrap());
assert_eq!(None, iter.next());Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Returns a double-ended iterator visiting all key-value pairs in order of insertion.
Iterator element type is (&'a K, &'a mut V)
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
{
let mut iter = map.iter_mut();
let mut entry = iter.next().unwrap();
assert_eq!(&"a", entry.0);
*entry.1 = 17;
}
assert_eq!(&17, map.get(&"a").unwrap());Sourcepub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
If the returned iterator is dropped before being fully consumed, it drops the remaining key-value pairs. The returned iterator keeps a mutable borrow on the vector to optimize its implementation.
Current performance implications (why to use this over into_iter()):
- Clears the inner HashMap instead of dropping it
- Puts all drained nodes in the free-list instead of deallocating them
- Avoids deallocating the sentinel node
Sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
Returns a double-ended iterator visiting all key in order of insertion.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);
let mut keys = map.keys();
assert_eq!(&'a', keys.next().unwrap());
assert_eq!(&'c', keys.next().unwrap());
assert_eq!(&'b', keys.next().unwrap());
assert_eq!(None, keys.next());Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
Returns a double-ended iterator visiting all values in order of insertion.
§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);
let mut values = map.values();
assert_eq!(&10, values.next().unwrap());
assert_eq!(&30, values.next().unwrap());
assert_eq!(&20, values.next().unwrap());
assert_eq!(None, values.next());Trait Implementations§
Source§impl<K, V, S> Clone for LinkedHashMap<K, V, S>
impl<K, V, S> Clone for LinkedHashMap<K, V, S>
Source§fn clone(&self) -> LinkedHashMap<K, V, S>
fn clone(&self) -> LinkedHashMap<K, V, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<A, B, S> Debug for LinkedHashMap<A, B, S>
impl<A, B, S> Debug for LinkedHashMap<A, B, S>
Source§impl<K, V, S> Default for LinkedHashMap<K, V, S>
impl<K, V, S> Default for LinkedHashMap<K, V, S>
Source§fn default() -> LinkedHashMap<K, V, S>
fn default() -> LinkedHashMap<K, V, S>
Source§impl<K, V, S> Drop for LinkedHashMap<K, V, S>
impl<K, V, S> Drop for LinkedHashMap<K, V, S>
Source§impl<'a, K, V, S> Extend<(&'a K, &'a V)> for LinkedHashMap<K, V, S>
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for LinkedHashMap<K, V, S>
Source§fn extend<I>(&mut self, iter: I)
fn extend<I>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V, S> Extend<(K, V)> for LinkedHashMap<K, V, S>
impl<K, V, S> Extend<(K, V)> for LinkedHashMap<K, V, S>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (K, V)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (K, V)>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V, S> FromIterator<(K, V)> for LinkedHashMap<K, V, S>
impl<K, V, S> FromIterator<(K, V)> for LinkedHashMap<K, V, S>
Source§fn from_iter<I>(iter: I) -> LinkedHashMap<K, V, S>where
I: IntoIterator<Item = (K, V)>,
fn from_iter<I>(iter: I) -> LinkedHashMap<K, V, S>where
I: IntoIterator<Item = (K, V)>,
Source§impl<K, V, S> Hash for LinkedHashMap<K, V, S>
impl<K, V, S> Hash for LinkedHashMap<K, V, S>
Source§impl<'a, K, V, S, Q> Index<&'a Q> for LinkedHashMap<K, V, S>
impl<'a, K, V, S, Q> Index<&'a Q> for LinkedHashMap<K, V, S>
Source§impl<'a, K, V, S, Q> IndexMut<&'a Q> for LinkedHashMap<K, V, S>
impl<'a, K, V, S, Q> IndexMut<&'a Q> for LinkedHashMap<K, V, S>
Source§impl<'a, K, V, S> IntoIterator for &'a LinkedHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a LinkedHashMap<K, V, S>
Source§impl<'a, K, V, S> IntoIterator for &'a mut LinkedHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut LinkedHashMap<K, V, S>
Source§impl<K, V, S> IntoIterator for LinkedHashMap<K, V, S>
impl<K, V, S> IntoIterator for LinkedHashMap<K, V, S>
Source§impl<K, V, S> Ord for LinkedHashMap<K, V, S>
impl<K, V, S> Ord for LinkedHashMap<K, V, S>
Source§fn cmp(&self, other: &LinkedHashMap<K, V, S>) -> Ordering
fn cmp(&self, other: &LinkedHashMap<K, V, S>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<K, V, S> PartialEq for LinkedHashMap<K, V, S>
impl<K, V, S> PartialEq for LinkedHashMap<K, V, S>
Source§impl<K, V, S> PartialOrd for LinkedHashMap<K, V, S>
impl<K, V, S> PartialOrd for LinkedHashMap<K, V, S>
Source§fn partial_cmp(&self, other: &LinkedHashMap<K, V, S>) -> Option<Ordering>
fn partial_cmp(&self, other: &LinkedHashMap<K, V, S>) -> Option<Ordering>
Source§fn lt(&self, other: &LinkedHashMap<K, V, S>) -> bool
fn lt(&self, other: &LinkedHashMap<K, V, S>) -> bool
Source§fn le(&self, other: &LinkedHashMap<K, V, S>) -> bool
fn le(&self, other: &LinkedHashMap<K, V, S>) -> bool
Source§fn ge(&self, other: &LinkedHashMap<K, V, S>) -> bool
fn ge(&self, other: &LinkedHashMap<K, V, S>) -> bool
impl<K, V, S> Eq for LinkedHashMap<K, V, S>
impl<K, V, S> Send for LinkedHashMap<K, V, S>
impl<K, V, S> Sync for LinkedHashMap<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for LinkedHashMap<K, V, S>where
S: Freeze,
impl<K, V, S> RefUnwindSafe for LinkedHashMap<K, V, S>
impl<K, V, S> Unpin for LinkedHashMap<K, V, S>where
S: Unpin,
impl<K, V, S> UnwindSafe for LinkedHashMap<K, V, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.