The BTreeMap is a B-tree data structure that stores key/value
pairs in an ordered way. See ProgramΒ 1.
The main features of a BTreeMap are:
The keys must implement the Ord trait.
Each node of a B-tree has a maximum of keys.
Iteration is made in key order, and has πͺ() complexity.
ββββββββ¬βββββββ¬βββββββ¬βββββββ
β10/"a"β16/"r"β23/"z"β β
ββββββββ΄βββββββ΄βββββββ΄βββββββ
β± β² β²
β± β² β²
ββββββββ¬βββββββ¬βββββββ¬βββββββ β ββββββββ¬βββββββ¬βββββββ¬βββββββ
β 2/"t"β 5/"e"β β β β β25/"w"β29/"o"β β β
ββββββββ΄βββββββ΄βββββββ΄βββββββ β ββββββββ΄βββββββ΄βββββββ΄βββββββ
ββββββββ¬βββββββ¬βββββββ¬βββββββ
β17/"w"β19/"o"β21/"j"β22/"v"β
ββββββββ΄βββββββ΄βββββββ΄βββββββProgramΒ 1:A BTreeMap with integer keys and string values, using a maximum of 4 keys per node.
The main methods of the BTreeMap are presented in
TableΒ 1.
TableΒ 1:Some methods of BTreeMap
Method | Description |
|---|---|
| Moves the elements of another map into this map. |
| Removes all elements. |
| Returns |
| Gives access to key/value pair. |
| Returns a reference to the value, if any. The returned type is
|
| Inserts a new key/value pair. |
| Returns |
| Returns an iterator on all key/value pairs. |
| Returns an iterator over the keys. Order is arbitrary. |
| Returns the number of elements. |
| Creates a new map. |
| Returns a double-ended iterator over a range of elements in the map. |
| Removes the specified key from the map. |
| Splits the map into two at the given key. The current map is modified to contain all elements with a key lower than the given key. Remaining elements are returned into a new map. |
| Returns an iterator over the values. Order is arbitrary. |
29.1.1Creating a new instanceΒΆ
To create a new hash map BTreeMap<K, V>, an easy way, like for the
Vec type, is to let the compiler infer the type. Inserting
at least one key/value pair will give the compiler the information it
needs:
use std::collections::BTreeMap;
let mut scores = BTreeMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);Couldn't automatically determine type of variable `scores`.
Please give it an explicit type.29.1.2Getting a valueΒΆ
Getting a value from the map is done using the get() method, which
returns an Option<V>:
let team_name = String::from("Blue");
match scores.get(&team_name) {
Some(score) => println!("The score of team {team_name} is {score}."),
None => println!("No score for team {team_name}"),
}BTreeMap implements the
Index trait, and
thus provides the indexing operator ([]) for retrieving a value.
Example:
scores["Blue"]29.1.3IteratingΒΆ
Using a for loop we can loop onto the key/value pairs:
for (k, v) in &scores {
println!("{k}: {v}");
}29.1.4Replacing an existing valueΒΆ
Inserting a key/value pair, replaces the eventual existing key:
scores.insert(String::from("Blue"), 34);
scores29.1.5Adding a value if no key presentΒΆ
The or_insert() of the Entry structure, allows to insert a
key/value only if the key is not already defined:
scores.entry(String::from("Green")).or_insert(18)29.1.6Updating an existing valueΒΆ
We suppose we want to increment the score of a team.
We retrieve a reference on the current score of team "Red", using
the or_insert() method in case the team is not declared, and
increment it by dereferencing it with the * operator. The
or_insert()
method returns a &mut V:
{
let count = scores.entry("Red".to_string()).or_insert(0);
*count += 1;
println!("count is now {count}");
}Here is the result:
scores