HashMap

最常见的集合是哈希映射。该类型HashMap 存储类型的键到类型K值的映射V。它通过散列函数完成此操作, 散列函数确定将这些键和值放入内存的方式。

  1. //============创建:
  2. use std::collections::HashMap;
  3. let mut scores = HashMap::new();
  4. scores.insert(String::from("Blue"), 10);
  5. scores.insert(String::from("Yellow"), 50);
  6. //方式二
  7. #![allow(unused_variables)]
  8. fn main() {
  9. use std::collections::HashMap;
  10. let teams = vec![String::from("Blue"), String::from("Yellow")];
  11. let initial_scores = vec![10, 50];
  12. let scores: HashMap<_, _> = teams.iter().zip(initial_scores.iter()).collect();
  13. }
  14. //==============访问: get 方法
  15. use std::collections::HashMap;
  16. let mut scores = HashMap::new();
  17. scores.insert(String::from("Blue"), 10);
  18. scores.insert(String::from("Yellow"), 50);
  19. let team_name = String::from("Blue");
  20. let score = scores.get(&team_name);
  21. //方式二
  22. use std::collections::HashMap;
  23. let mut scores = HashMap::new();
  24. scores.insert(String::from("Blue"), 10);
  25. scores.insert(String::from("Yellow"), 50);
  26. for (key, value) in &scores {
  27. println!("{}: {}", key, value);
  28. }
  29. //==============更新
  30. //方式一: 覆盖值
  31. use std::collections::HashMap;
  32. let mut scores = HashMap::new();
  33. scores.insert(String::from("Blue"), 10);
  34. scores.insert(String::from("Blue"), 25);
  35. println!("{:?}", scores);
  36. //方式二:仅在键没有值时插入值
  37. //通常检查特定键是否具有值,如果不具有,则为其插入值。 散列映射为此被调用条目提供了一个特//殊的API,它将您要检查的键作为参数。 entry函数的返回值是一个名为Entry的枚举,表示可能存//在或可能不存在的值。
  38. #![allow(unused_variables)]
  39. fn main() {
  40. use std::collections::HashMap;
  41. let mut scores = HashMap::new();
  42. scores.insert(String::from("Blue"), 10);
  43. scores.entry(String::from("Yellow")).or_insert(50);
  44. scores.entry(String::from("Blue")).or_insert(50);
  45. println!("{:?}", scores);
  46. }
  47. //方式三:基于旧值更新值,常见用例是查找键的值,然后根据旧值更新它
  48. #![allow(unused_variables)]
  49. fn main() {
  50. use std::collections::HashMap;
  51. let text = "hello world wonderful world";
  52. let mut map = HashMap::new();
  53. for word in text.split_whitespace() {
  54. let count = map.entry(word).or_insert(0);
  55. *count += 1;
  56. }
  57. println!("{:?}", map);
  58. }
  59. //===============删除: 根据key删除元素
  60. come_from.remove("Mike");
  61. println!("Mike猫的家乡不是火锅!不是火锅!不是火锅!虽然好吃!");

哈希函数

默认情况下,HashMap使用加密安全散列函数,可以抵御拒绝服务(DoS)攻击。 这不是最快的哈希算法,但是随着性能下降而带来更好的安全性的权衡是值得的。 如果您对代码进行概要分析并发现默认哈希函数对于您的目的而言太慢,则可以通过指定其他哈希来切换到另一个函数。 hasher是一种实现BuildHasher特征的类型。

HashMap 不保证键的順序,依其鍵/值对来访问,

  1. use std::collections::HashMap;
  2. fn main() {
  3. let mut hash = HashMap::new();
  4. hash.insert("one", "eins");
  5. hash.insert("two", "zwei");
  6. hash.insert("three", "drei");
  7. // Iterate the hash by key
  8. for k in hash.keys() {
  9. println!("{} => {}", k, *hash.get(k).unwrap());
  10. }
  11. }
  1. use std::collections::HashMap;
  2. fn main() {
  3. let mut hash = HashMap::new();
  4. hash.insert("one", "eins");
  5. hash.insert("two", "zwei");
  6. hash.insert("three", "drei");
  7. // Iterate the hash by (key, value) pair
  8. for (k, v) in hash.iter() {
  9. println!("{} => {}", k, *v);
  10. }
  11. }

也可依其值来访问,要注意的是,只能由鍵推得值,无法由值推得鍵。鍵是唯一的,但值可以重复。

  1. use std::collections::HashMap;
  2. fn main() {
  3. let mut hash = HashMap::new();
  4. hash.insert("one", "eins");
  5. hash.insert("two", "zwei");
  6. hash.insert("three", "drei");
  7. // Iterate the hash by value
  8. for v in hash.values() {
  9. println!("{}", *v);
  10. }
  11. }