简体   繁体   中英

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. 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
  • use Apache Commons Collections MultiMap
  • use a Map<Key, Set<Value>> or any other collection for the values that would meet your needs

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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