Struct near_sdk::collections::unordered_map::UnorderedMap
source · pub struct UnorderedMap<K, V> { /* private fields */ }
Expand description
An iterable implementation of a map that stores its content directly on the trie.
Implementations§
source§impl<K, V> UnorderedMap<K, V>
impl<K, V> UnorderedMap<K, V>
sourcepub fn len(&self) -> u64
pub fn len(&self) -> u64
Returns the number of elements in the map, also referred to as its size.
§Examples
use near_sdk::collections::UnorderedMap;
let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
assert_eq!(map.len(), 0);
map.insert(&1, &1);
map.insert(&2, &2);
assert_eq!(map.len(), 2);
sourcepub fn new<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
pub fn new<S>(prefix: S) -> Selfwhere
S: IntoStorageKey,
Create new map with zero elements. Use prefix
as a unique identifier.
§Examples
use near_sdk::collections::UnorderedMap;
let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
sourcepub fn insert_raw(
&mut self,
key_raw: &[u8],
value_raw: &[u8]
) -> Option<Vec<u8>>
pub fn insert_raw( &mut self, key_raw: &[u8], value_raw: &[u8] ) -> Option<Vec<u8>>
Inserts a serialized key-value pair into the map.
If the map did not have this key present, None
is returned. Otherwise returns
a serialized value. Note, the keys that have the same hash value are undistinguished by
the implementation.
source§impl<K, V> UnorderedMap<K, V>
impl<K, V> UnorderedMap<K, V>
sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Returns the value corresponding to the key.
§Examples
use near_sdk::collections::UnorderedMap;
let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
assert_eq!(map.get(&1), None);
map.insert(&1, &10);
assert_eq!(map.get(&1), Some(10));
sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
§Examples
use near_sdk::collections::UnorderedMap;
let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
assert_eq!(map.remove(&1), None);
map.insert(&1, &10);
assert_eq!(map.remove(&1), Some(10));
assert_eq!(map.len(), 0);
sourcepub fn insert(&mut self, key: &K, value: &V) -> Option<V>
pub fn insert(&mut self, key: &K, value: &V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None
is returned. Otherwise returns
a value. Note, the keys that have the same hash value are undistinguished by
the implementation.
§Examples
use near_sdk::collections::UnorderedMap;
let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
map.insert(&1, &10);
assert_eq!(map.get(&1), Some(10));
assert_eq!(map.len(), 1);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all elements.
§Examples
use near_sdk::collections::UnorderedMap;
let mut map: UnorderedMap<u8, u8> = UnorderedMap::new(b"m");
map.insert(&1, &10);
map.insert(&2, &20);
map.clear();
assert_eq!(map.len(), 0);
sourcepub fn keys(&self) -> impl Iterator<Item = K> + '_
pub fn keys(&self) -> impl Iterator<Item = K> + '_
An iterator visiting all keys. The iterator element type is K
.
sourcepub fn values(&self) -> impl Iterator<Item = V> + '_
pub fn values(&self) -> impl Iterator<Item = V> + '_
An iterator visiting all values. The iterator element type is V
.
pub fn extend<IT: IntoIterator<Item = (K, V)>>(&mut self, iter: IT)
sourcepub fn keys_as_vector(&self) -> &Vector<K>
pub fn keys_as_vector(&self) -> &Vector<K>
Returns a view of keys as a vector. It’s sometimes useful to have random access to the keys.
sourcepub fn values_as_vector(&self) -> &Vector<V>
pub fn values_as_vector(&self) -> &Vector<V>
Returns a view of values as a vector. It’s sometimes useful to have random access to the values.