Crate collecting_hashmap
source ·Expand description
Collecting HashMap
This is a HashMap that allows the user to store multiple V
values for each K
key. Currently
it is implemented by internally keeping a HashMap<K, Vec<V>>
and forwarding most operations
to that HashMap
. There area few calls where it does more than just forward, in order to keep
the API as functionally similar to a regular HashMap<K, V>
as possible. The main difference
is with the insert
method. Since it won’t replace the original value if you insert
another
value for the same K
, this insert
returns nothing.
The get
and get_mut
methods have the same signature as a regular HashMap<K, V>
. Instead
of returning the whole underlying Vec
for a key, get
and get_mut
both return a reference
to the first item in the Vec
. In order to get a reference to the whole Vec
for a key, use
get_all
and get_all_mut
.
The Entry
API operates on the entire underlying Vec
for a key.
Examples
use collecting_hashmap::CollectingHashMap;
let mut map = CollectingHashMap::new();
map.insert("voltron", "black");
map.insert("voltron", "red");
map.insert("voltron", "green");
map.insert("voltron", "blue");
map.insert("voltron", "yellow");
assert_eq!(map.get_all("voltron"), Some(&vec!["black", "red", "green", "blue", "yellow"]));
use collecting_hashmap::CollectingHashMap;
let query_string = vec![
("q", "query1"),
("t", "0m2s"),
("q", "query2"),
("q", "query3"),
];
let map = query_string.into_iter().collect::<CollectingHashMap<_, _>>();
assert_eq!(map.get_all("q"), Some(&vec!["query1", "query2", "query3"]));