简体   繁体   English

Java中HashMap中一个键的多个值

[英]Multiple values for a key in HashMap in Java

Is it possible to keep multiple values corresponding to a key in a HashMap?是否可以保留与 HashMap 中的键相对应的多个值? If yes, how?如果是,如何?

Yes, this is called chaining.是的,这称为链接。 You will want to avoid chaining as much as possible, especially if the size of the chain starts increasing.您将希望尽可能避免链接,特别是当链的大小开始增加时。 Longer chain size will counter the whole purpose of using a hash structure because the goal is to come as close to O(1) as possible.更长的链大小将违背使用哈希结构的整个目的,因为目标是尽可能接近 O(1)。

Map<String, List<String>> hm = new HashMap<String, List<String>>();
List<String> values = new ArrayList<String>();
values.add("Value 1");
values.add("Value 2");
hm.put("Key1", values);

You could give a shot at Guava library (former Google collections).你可以试一试番石榴图书馆(前谷歌收藏)。 It has implementations of Multimaps which can store multiple values for a single key.它具有 Multimaps 的实现,可以为单个键存储多个值。

For example ListMultimap implementations allow duplicate key/value pairs which are kept in insertion order.例如ListMultimap实现允许按插入顺序保存的重复键/值对。

Here's how you'd use it:以下是您如何使用它:

ListMultimap<String, Integer> numberClasses = ArrayListMultimap.create();
numberClasses.put("odd", 1);
numberClasses.put("odd", 3);
numberClasses.put("odd", 5);

numberClasses.put("even", 2);
numberClasses.put("even", 4);
numberClasses.put("even", 6);

assertEquals(Arrays.asList(1,3,5), numberClasses.get("odd"));
assertEquals(Arrays.asList(2,4,6), numberClasses.get("even"));

Another cool example would be SetMultimap , which is very similar to ListMultimap except that values for a key are kept in a set.另一个很酷的例子是SetMultimap ,它与 ListMultimap 非常相似,只是键的值保存在一个集合中。 (From user perspective, I don't know how exactly it is implemented.) (从用户的角度来看,我不知道它是如何实现的。)

SetMultimap<String, Integer> setMultimap= HashMultimap.create();
setMultimap.put("key1", 1);
setMultimap.put("key1", 1);
setMultimap.put("key1", 1);
setMultimap.put("key1", 2);

setMultimap.put("key2", 1);
setMultimap.put("key2", 3);

assertEquals(ImmutableSet.of(1,2), setMultimap.get("key1"));
assertEquals(ImmutableSet.of(1,3), setMultimap.get("key2"));

使用Map<String, List<String>>

Strictly speaking, no.严格来说,没有。

But!但! You can have as your value, some kind of Collection and use that to store as many values as you wish.您可以将某种Collection作为您的价值,并使用它来存储您希望的任意数量的价值。

Yes, but only if the value type being stored in your Map is an array or List:是的,但前提是存储在Map的值类型是数组或列表:

Map<String, List<String>> myMap

or要么

Map<String, String[]> myMap

But it's generally bad practice to build up generic data structures inside generic data structures.但是在通用数据结构中构建通用数据结构通常是不好的做法。

Why not write a domain-specific class that wraps the HashMap , and makes it easier for you to check for existence of a value, number of items per key, etc?为什么不编写一个包含HashMap的特定于域的类,并使您更容易检查值是否存在、每个键的项目数等?

Without using any other Libary Create a map with Key as String and value as HashSet value will be not repeated string.不使用任何其他 Libary 创建一个以 Key 为 String 和 value 为 HashSet 值的映射将不会重复字符串。


multivaleMap.computeIfAbsent("key", k -> new HashSet<String>()).add("value1");
multivaleMap.computeIfAbsent("key", k -> new HashSet<String>()).add("value2");

System.out.println(multivaleMap);``` will print {key=[value2, value1]}

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

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