简体   繁体   English

如何在 Rust HashMap 中为同一个键存储多个元素?

[英]How can I store multiple elements in a Rust HashMap for the same key?

I have a HashMap<u32, Sender> .我有一个HashMap<u32, Sender> Sender is a open connection object and the key is a user id. Sender是一个开放的连接对象,key 是一个用户 ID。 Each user can connect from multiple devices.每个用户可以从多个设备连接。 I need to store all possible open connections for the same user id.我需要为同一个用户 ID 存储所有可能的打开连接。 After this I can iterate and send messages to all open connections for same user.在此之后,我可以迭代并向同一用户的所有打开的连接发送消息。

The above HashMap only stores each user id and connection once.上面的HashMap只存储每个用户 id 和连接一次。 I need to get one key with multiple values.我需要获得一个具有多个值的键。 How can I make the value into a list or an array, so I can see which connections exist and send to them all?如何将值放入列表或数组中,以便我可以查看存在哪些连接并将其发送给所有连接?

I am not talking about different value types, like enums.我不是在谈论不同的值类型,比如枚举。 I am talking about the same type values but more than one.我说的是相同类型的值,但不止一个。 Maybe HashMap is not designed for this?也许HashMap不是为此而设计的?

Alternative ideas are also welcomed.也欢迎其他想法。

To do this with a HashMap you should use a Vec as the values, so that each key can point to multiple Sender s.要使用HashMap执行此操作,您应该使用Vec作为值,以便每个键可以指向多个Sender The type then would be HashMap<u32, Vec<Sender>> .然后类型将是HashMap<u32, Vec<Sender>>

Using this structure, just using insert() can get clunky when you need to mutate the values like this, but instead you can use the Entry API for retrieving and updating records in one go.使用这种结构,当您需要像这样改变值时,仅使用insert()会变得笨拙,但是您可以使用Entry API 一次性检索和更新记录。 For example:例如:

let mut hash_map: HashMap<u32, Vec<Sender>> = HashMap::new();

hash_map.entry(3)
    // If there's no entry for key 3, create a new Vec and return a mutable ref to it
    .or_default()
    // and insert the item onto the Vec
    .push(sender); 

You could also use the multimap crate , which does something similar under the hood, but adds a layer of abstraction.您也可以使用multimap crate ,它在multimap做类似的事情,但增加了一个抽象层。 You might find it easier to work with:您可能会发现使用以下工具更容易:

let mut multi_map = MultiMap::new();

multi_map.insert(3, sender_1);
multi_map.insert(3, sender_2);

The method multi_map.get(key) will the first value with that key, while multi_map.get_vec(key) will retrieve all of them.方法multi_map.get(key)将使用该键的第一个值,而multi_map.get_vec(key)将检索所有这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C-如何在哈希图中为同一个键分配多个值? - C - How to assign multiple values to the same key in a hashmap? 如何仅在4位中存储0-9范围内的整数并使用与HashMap中的Key相同的数字? - How to store integer numbers in the range of 0-9 in only 4 bits and use the same as Key in HashMap? 如何通过键列表对Hashmap中的列表元素进行分组 - How to group List elements in Hashmap by a List of key 如何将值列表合并到 hashmap 中的相同键? - how to merge the list of values to the same Key in hashmap? 如何在Java中获取HashMap的相同值多次出现次数? - how can i get the number of same-value-more-than-once occurrences for a HashMap in Java? 如何存储具有相同父键的值 - how to store the values that has the same parent key 如何在 TRIE 结构中为一个键存储多个值? - How to store multiple values for a key in a TRIE structure? 多次使用相同密钥的红黑树:在节点中存储集合或将它们存储为多个节点? - A red black tree with the same key multiple times: store collections in the nodes or store them as multiple nodes? 如何使用从 Hashmap 上的 Arraylist 中选择的值? - How can I use a value I chose from the Arraylist on Hashmap? 如何按级别顺序打印 Hashmap 中包含相同值的键值 - How to print the key values which contains same values in the Hashmap in Level Order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM