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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:31:09  来源:igfitidea点击:

duplicate key value pairs

javadata-structures

提问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 Characterobject 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 keyis the Character and valueits corresponding Booleanvalue.

其中key是字符value及其对应的Boolean值。

The Guavalibrary 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 Collectionslibrary and use its MultiValueMapclass. 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 Setshould 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 Listof Pairs:

使用 aListPairs:

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 Pairclass 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.collectionspackage.

您可以使用MultiMap<Character,Boolean>bcoz 它允许org.apache.commons.collections包中存在的重复键。

or

或者

You can use ArrayListand add the objects of the Classthat 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));
        }
    }
}