哪个 java 集合允许重复键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35641702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which java collection allows duplicate keys
提问by amal
Im trying to write program to remove duplicate key-value pairs of a word list. However if key is duplicated with different value, that record should be allowed to add. Please help me to understand Which java collection would be solve this situation.
我正在尝试编写程序来删除单词列表的重复键值对。但是,如果键与不同的值重复,则应允许添加该记录。请帮助我了解哪个 java 集合可以解决这种情况。
- key1 aaaa
- key2 bbbb
- key3 cccc
- key4 dddd
- key2 bbbb - duplicate pair - not allowed
- key1 hhhh - duplicate key - allowed
- key5 gggg
- key2 nnnn
- 键 1 aaaa
- key2 bbbb
- key3 cccc
- key4 dddd
- key2 bbbb - 重复对 - 不允许
- key1 hhhh - 重复键 - 允许
- 关键5 gggg
- 键 2 nnnn
采纳答案by konkked
You can do this with a multimap, using a set as the collection for the values, it is fairly simple to make.
您可以使用多图来做到这一点,使用集合作为值的集合,制作起来相当简单。
Here is some of the basics of an implementation, not the whole thing but couldn't imagine you would need more than that or maybe a remove method
这是实现的一些基础知识,不是全部,但无法想象您还需要更多或者可能需要删除方法
Edit
编辑
Just saw you wanted duplicate pairs to be thrown away, can do that using a set, instead of throwing an error just gave back the bool to show if it was there already or not (if it exists returns false)
刚刚看到您想要丢弃重复的对,可以使用集合来做到这一点,而不是抛出错误,只需返回 bool 以显示它是否已经存在(如果存在则返回 false)
public class MultiValueMap<K,V>
{
private final Map<K,Set<V>> mappings = new HashMap<K,Set<V>>();
public Set<V> getValues(K key)
{
return mappings.get(key);
}
public Boolean putValue(K key, V value)
{
Set<V> target = mappings.get(key);
if(target == null)
{
target = new HashSet<V>();
mappings.put(key,target);
}
return target.add(value);
}
}
回答by sanky jain
You cannot do it by Java collection.
你不能通过 Java 集合来做到这一点。
You can use Multimap it supports duplicate keys but it also support duplicate keys and value pairs.
您可以使用 Multimap 它支持重复键但它也支持重复键和值对。
Best solution for you is use Multimap and check if value already exist then dont add it.
最适合您的解决方案是使用 Multimap 并检查值是否已存在,然后不要添加它。
回答by Stefan Dollase
To my knowledge, there is no such collection implementation in the default JRE. However, there seem to be implementations in third party libraries.
据我所知,默认 JRE 中没有这样的集合实现。但是,第三方库中似乎有实现。
To get something similar, you can use a Map<K, List<V>>
, which is a map containing a list of values for each key.
要获得类似的结果,您可以使用 a Map<K, List<V>>
,它是一个包含每个键值列表的映射。
However, I do not think that you need this. To merge values for duplicate keys you can check whether the key already exists before you put a new key-value pair into the map.
但是,我认为您不需要这个。要合并重复键的值,您可以在将新键值对放入映射之前检查该键是否已存在。
- if it already exists, replace the value by the merged old and new value
- if it does not yet exist, just put the new key-value pair into the map.
- 如果它已经存在,则用合并的旧值和新值替换该值
- 如果它还不存在,只需将新的键值对放入映射中。
回答by Arif Burhan
A dictionary contains words and definitions, so sheep="wooly mammal" is a valid assignment. Each time you look up sheep you get wooly mammal.
字典包含单词和定义,因此sheep="wooly motor" 是一个有效的赋值。每次您查找绵羊时,您都会看到毛茸茸的哺乳动物。
An array is indexed by an integer, and can have duplicate values,
数组由整数索引,并且可以有重复的值,
arr[2]=5 ; arr[7]=5;
A hash can also store duplicate values, but the keys must be unique:
散列也可以存储重复值,但键必须是唯一的:
Adam{age}=21;
Bill{age}=21;
Some languages use dots for properties:
一些语言使用点作为属性:
Adam.age=21;
回答by rootExplorr
Your case basically needs a HashMap.
你的情况基本上需要一个HashMap。
Just put key as key and value as value in a HashMap.
只需将键作为键,将值作为值放在 HashMap 中。
This is because key will any ways be unique and in case there is collision in values HashMap maintains a linked list for storing all these colliding values.In case any value is same as any earlier value in the linked list it simply replaces the old one with the new one.
这是因为 key 的任何方式都是唯一的,如果值发生冲突,HashMap 维护一个链表来存储所有这些冲突值。新的那一个。
For ex.
例如。
As per your requirement :
根据您的要求:
Key1 aaaa -- should be stored Key1 bbbb -- should be stored Key1 aaaa -- should not be stored as it is duplicate.
Key1 aaaa -- 应该存储 Key1 bbbb -- 应该存储 Key1 aaaa -- 不应该存储,因为它是重复的。
So basically hashmap will store "aaaa" and "bbbb"values against "key1" as the key. Later when we try storing "aaaa" again against "key1" then older stored value "aaaa" will simply be replaced.
所以基本上hashmap会将“aaaa”和“bbbb”值存储为“key1”作为键。稍后当我们再次尝试针对“key1”存储“aaaa”时,旧的存储值“aaaa”将被简单地替换。
Hence, duplicacy of values is automatically handled by hashmap.
因此,值的重复由 hashmap 自动处理。
Hence , you can make use of HashMap in your case.
因此,您可以在您的情况下使用 HashMap。
回答by Brian
Since Guava 2.0, there's a new map type, the SetMultimap, that you can use that I think fits your purposes exactly. It allows duplicate keys, but not duplicate key/value pairs. See the Guava documentation.
从 Guava 2.0 开始,有一种新的地图类型 SetMultimap,您可以使用它,我认为它完全符合您的目的。它允许重复键,但不允许重复键/值对。请参阅番石榴文档。