·键值对数据又称字典数据类型
·主要有两种
- · HashMap
·- BTreeMap
1.HashMap
·HashMap<K,V>类型储存了一个键类型K对应一个值类型V的映射。它通过一个 哈希函数(hashing function)来实现映射,决定如何将键和值放入内存中。
·HashMap的数据和Vec一样在heap上
2.什么时候用HashMap
·仅次于Vec的常用数据类型
·存储数据为键值对类型
需要查找的速度
- in-memory cache
3.HashMap中的键
·因为要满足哈希函数,所以HashMap对键有特殊要求
·实现Hash、Eq、PartialEq
·一般结构体: #[derive(Debug, PartialEq, Hash, Eq)]
use std::collections::HashMap;
// Hash Eq PartialEq
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Car {
id: i32,
price: i32,
}
fn main() {
let _int_map: HashMap<i32, i32> = HashMap::new();
let _int_map: HashMap<i32, i32> = HashMap::with_capacity(10);
// 通过数组来创建map
let mut car_map = HashMap::from([
(
"Car1",
Car {
id: 1,
price: 10000,
},
),
("Car2", Car { id: 2, price: 4000 }),
(
"Car3",
Car {
id: 3,
price: 890000,
},
),
]);
// 打印实际是无序的
for (k, v) in &car_map {
println!("{k}:{:?}", v);
}
// get
println!("Some {:?}", car_map.get("Car1"));
println!("None {:?}", car_map.get("Car6"));
// 覆盖性插入insert
car_map.insert(
"Car4",
Car {
id: 4,
price: 80000,
},
);
println!("{:?}", car_map);
car_map.insert(
"Car4",
Car {
id: 5,
price: 300000,
},
);
println!("{:?}", car_map);
// 只在键没有时插入
// Entry
car_map.entry("Car4").or_insert(Car { id: 9, price: 9000 });
println!("{:?}", car_map);
// remove
car_map.remove("Car4");
println!("{:?}", car_map);
car_map.entry("Car4").or_insert(Car { id: 9, price: 9000 });
println!("{:?}", car_map);
// 加上注释PartialEq, Eq, Hash
let mut car_map = HashMap::from([
(
Car {
id: 1,
price: 10000,
},
"Car1",
),
(Car { id: 2, price: 4000 }, "Car2"),
(
Car {
id: 3,
price: 890000,
},
"Car3",
),
]);
println!(
"Car2: {:?}\n",
car_map.get(&Car {
id: 1,
price: 10000
})
);
for (car, name) in &car_map {
println!("{:?}: {name}", car)
}
// Filter:会原地修改map
car_map.retain(|c, _| c.price < 5000);
println!("< 4000 {:?}", car_map);
}
编译及运行
cargo run
Blocking waiting for file lock on build directory
Compiling data_struct v0.1.0 (/home/wangji/installer/rust/data_struct/data_struct)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 13.52s
Running `target/debug/data_struct`
Car1:Car { id: 1, price: 10000 }
Car3:Car { id: 3, price: 890000 }
Car2:Car { id: 2, price: 4000 }
Some Some(Car { id: 1, price: 10000 })
None None
{"Car4": Car { id: 4, price: 80000 }, "Car2": Car { id: 2, price: 4000 }, "Car1": Car { id: 1, price: 10000 }, "Car3": Car { id: 3, price: 890000 }}
{"Car4": Car { id: 5, price: 300000 }, "Car2": Car { id: 2, price: 4000 }, "Car1": Car { id: 1, price: 10000 }, "Car3": Car { id: 3, price: 890000 }}
{"Car4": Car { id: 5, price: 300000 }, "Car2": Car { id: 2, price: 4000 }, "Car1": Car { id: 1, price: 10000 }, "Car3": Car { id: 3, price: 890000 }}
{"Car2": Car { id: 2, price: 4000 }, "Car1": Car { id: 1, price: 10000 }, "Car3": Car { id: 3, price: 890000 }}
{"Car4": Car { id: 9, price: 9000 }, "Car2": Car { id: 2, price: 4000 }, "Car1": Car { id: 1, price: 10000 }, "Car3": Car { id: 3, price: 890000 }}
Car2: Some("Car1")
Car { id: 3, price: 890000 }: Car3
Car { id: 1, price: 10000 }: Car1
Car { id: 2, price: 4000 }: Car2
< 4000 {Car { id: 2, price: 4000 }: "Car2"}