Java 重复键值对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21586690/
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
duplicate key value pairs
提问by Liondancer
Is there a native data structure in java that accepts key value pairs and allows duplicates? I am creating a checklist of characters in a string but some characters occur more than once.
java中是否有接受键值对并允许重复的本机数据结构?我正在创建一个字符串中的字符清单,但有些字符出现不止一次。
ex
前任
j -> false
a -> false
v -> false
a -> false
采纳答案by PNS
You can simulate multiple key-value (KV) pairs by saving a list of values for each in a map. This is a standard implementation approach for "multivalue" maps.
您可以通过在映射中保存每个键值 (KV) 对的值列表来模拟多个键值 (KV) 对。这是“多值”映射的标准实现方法。
So, if the key is a Character
object and the value is Boolean
, you can do
所以,如果键是一个Character
对象,值是Boolean
,你可以这样做
Map<Character, List<Boolean>> multimap = new HashMap<Character, List<Boolean>>();
and every time you want to add a new value to an existing KV pair in the map just call
每次您想向地图中的现有 KV 对添加新值时,只需调用
multimap.get(key).add(value);
where key
is the Character and value
its corresponding Boolean
value.
其中key
是字符value
及其对应的Boolean
值。
The Guava
library by Google
(free download) has a Multimap interfaceimplemented in various ways, so essentially you can instantiate a MultiMap<Character, Boolean>
map and use it accordingly. Similarly, you can get the Apache Commons Collections
library and use its MultiValueMap
class. You may also want to check the answers to a similar StackOverflow question, or another one.
该Guava
库由Google
(免费下载)具有以各种方式实现的Multimap 接口,因此基本上您可以实例化MultiMap<Character, Boolean>
地图并相应地使用它。同样,您可以获取Apache Commons Collections
库并使用其MultiValueMap
类。您可能还想查看类似 StackOverflow 问题或其他问题的答案。
If you only want to store one of each value per key, then a Set
should be used in the place of the List
.
如果您只想为每个键存储每个值中的一个,Set
则应使用a代替List
.
回答by useSticks
I do not know of a build in solution.
我不知道内置解决方案。
A quick alternative would be to use a simple ArrayList, and create an object that is a char/boolean pair that you can add to it.
一个快速的替代方法是使用一个简单的 ArrayList,并创建一个可以添加到它的 char/boolean 对的对象。
回答by bobbel
Use a List
of Pair
s:
使用 aList
的Pair
s:
public class Pair<T, U> {
public final T key;
public final U value;
public Pair(T key, U value) {
this.key = key;
this.value = value;
}
}
public class YourApp {
public static void main(String[] args) {
List<Pair<Character, Boolean>> charList = new ArrayList<Pair<Character, Boolean>>();
charList.add(new Pair('j', false));
charList.add(new Pair('a', false));
charList.add(new Pair('v', false));
charList.add(new Pair('a', false));
for (Pair<Character, Boolean> pair : charList) {
System.out.println(pair.key + " -> " + pair.value);
}
}
}
With the selfwritten generic Pair
class you can hold a key and a value of any type you want. If you're adding pairs to a List
, you can even hold duplicates of pair entries.
使用自写的泛型Pair
类,您可以保存您想要的任何类型的键和值。如果您将对添加到 a List
,您甚至可以保存对条目的重复项。
回答by Kick
You can use MultiMap<Character,Boolean>
bcoz it allows duplicate key which exist in org.apache.commons.collections
package.
您可以使用MultiMap<Character,Boolean>
bcoz 它允许org.apache.commons.collections
包中存在的重复键。
or
或者
You can use ArrayList
and add the objects of the Class
that contain attribute as char & boolean pair.
您可以使用ArrayList
和添加Class
包含属性的对象作为字符和布尔值对。
回答by Akhilesh Dhar Dubey
commons.apache.org have MultiHashMap class. Try this one...!!!
commons.apache.org 有 MultiHashMap 类。试试这个……!!!
MultiHashMap mp = new MultiHashMap();
mp.put("a", "1");
mp.put("b", "4");
mp.put("c", "2");
mp.put("a", "6");
List list = null;
Set set = mp.entrySet();
Iterator i = set.iterator();
while (i.hasNext()) {
Map.Entry<String, List<String>> me = (Map.Entry) i.next();
for(int j = 0 ; j< me.getValue().size(); j++ ){
System.out.println(me.getKey() +" : " +me.getValue().get(j));
}
}
}