简体   繁体   English

如何在TreeMap中向同一个键添加多个值?

[英]How do I add multiple values to the same key in a TreeMap?

My tree Map is 我的树地图是

Map<String, Double> restrMap = new TreeMap<String, Double>(); 

While adding the below two value to the treeMap, it only shows one. 在将以下两个值添加到treeMap时,它只显示一个。 The second value, when comes updates the first one. 第二个值,何时更新第一个值。

6, 8.00
6, 5.00

How can I add two values for the same key, perhaps in different rows? 如何为同一个键添加两个值,可能在不同的行中?

如果要向同一个键添加多个值,请考虑使用列表映射。

Map<String, List<Double>> restrMap = new TreeMap<String, List<Double>>();

A map has only one value associated to a specific key. 地图只有一个与特定键关联的值。

If you want several values, you can: 如果您想要多个值,您可以:

  • use Guava's Multimap 使用Guava的Multimap
  • use Apache Commons Collections MultiMap 使用Apache Commons Collections MultiMap
  • use a Map<Key, Set<Value>> or any other collection for the values that would meet your needs 使用Map<Key, Set<Value>>或任何其他集合来获取满足您需求的值

Java没有multimap,但您可以在map值中使用另一个容器。

Map<String, List<Double>> restrMap = new TreeMap<String, List<Double>>(); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM