简体   繁体   English

Java HashMultiMap存储问题

[英]Java HashMultiMap Storing Issue

I am using HashMultiMap in my code. 我在我的代码中使用HashMultiMap My HashMultiMap structure is like, 我的HashMultiMap结构就像,

Key1 -> Value11 -> Value12 -> Value13 .... Key1 - > Value11 - > Value12 - > Value13 ....

Key2 -> Value21 -> Value22 .... Key2 - > Value21 - > Value22 ....

Now, I want if key1 has same values (example: value11 = value12) then keep (or do not store) only one copy of the value (example: keep only value11 or do not store value12). 现在,我想如果key1具有相同的值(例如:value11 = value12),则保留(或不存储)该值的一个副本(例如:仅保留value11或不存储value12)。 Can anybody help me how to achieve this with an efficient (faster) way. 任何人都可以帮助我以高效(更快)的方式实现这一目标。

According to the HashMultiMap Javadoc, you chose the right MultiMap for that purpose: 根据HashMultiMap Javadoc,您为此目的选择了正确的MultiMap:

The multimap does not store duplicate key-value pairs. 多图不存储重复的键值对。 Adding a new key-value pair equal to an existing key-value pair has no effect. 添加等于现有键值对的新键值对无效。

Now, you only have to make sure that equals() (and hashCode() ) is implemented correctly on your values. 现在,您只需要确保在您的值上正确实现了equals() (和hashCode() )。 I don't think you should worry about a faster way to do this. 我认为你不应该担心更快的方法。 The HashMultiMap should be implemented pretty efficiently. 应该非常有效地实现HashMultiMap

If you want this behavior why not use a structure like: 如果您想要这种行为,为什么不使用如下结构:

Map<Key,Set<Values>> myMap = new HashMap<Key,Set<Values>>();

EDIT: 编辑:

If you want to use the HashMultiMap i would recommend the one bellow 如果你想使用HashMultiMap,我会推荐下面的那个

Implementation of the MultiMap interface that uses a HashMap for the map and HashSets for automatically created sets. 实现MultiMap接口,该接口使用HashMap作为地图,HashSets使用自动创建的集合。

http://people.csail.mit.edu/milch/blog/apidocs/common/HashMultiMap.html http://people.csail.mit.edu/milch/blog/apidocs/common/HashMultiMap.html

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

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