java中的多值映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30704568/
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-11 10:02:05  来源:igfitidea点击:

MultiValueMap in java

javacollectionshashmap

提问by user3810857

I'm studying with Hashmap with Multiple parameters(1 key, 2 values) and i was able to find apache multiValueMap for my issue.

我正在研究具有多个参数(1 个键,2 个值)的 Hashmap,我能够找到 apache multiValueMap 来解决我的问题。

Here is my codes for multiValueMap.

这是我的 multiValueMap 代码。

import java.util.Set;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.collections.MultiMap;

public class multiValueMap {

public static void main(String args[]) {
   String a, b, c;
   MultiMap mMap = new MultiValueMap();

   mMap.put("a", "Hello there, It's a wonderful day");
   mMap.put("a", "nice to meet you");

   Set<String> keys = mMap.keySet();

   for (String key : keys) {
      System.out.println("Key = " + key);
      System.out.println("Values = " + mMap.get(key));
      a = String.valueOf(mMap.get(key));

      System.out.println("A : " + a);
    }
 }
}
// The result as below
 Key = a 
 Value = [Hello there, It's a wonderful day, nice to meet you]
 A : [Hello there, It's a wonderful day, nice to meet you]

Here is my question how can I store first value for string b, and second for c?if I substring the MultiMap values depends on "," then it would stores Hello there only. please give me helpful your advices.

这是我的问题, 如何为字符串 b 存储第一个值,为 c 存储第二个值?如果我将 MultiMap 值子字符串取决于“,”,那么它只会在那里存储 Hello。请给我有用的建议。

采纳答案by Sach141

You can try following :

您可以尝试以下操作:

String a, b, c;

MultiMap mMap = new MultiValueMap();
mMap.put("a", "Hello there, It's a wonderful day");
mMap.put("a", "nice to meet you");

Set<String> keys = mMap.keySet();

for (String key : keys) {
    System.out.println("Key = " + key);
    System.out.println("Values = " + mMap.get(key));
    List<String> list = (List<String>) mMap.get(key);

    b = list.get(0);
    c = list.get(1);
    System.out.println("B : " + b);
    System.out.println("C : " + c);
} 

回答by Praba

You don't have to do a split. This is the documentation of MultiMap that is found:

您不必进行拆分。这是找到的 MultiMap 的文档:

MultiMap mhm = new MultiHashMap();
 mhm.put(key, "A");
 mhm.put(key, "B");
 mhm.put(key, "C");
 Collection coll = (Collection) mhm.get(key);

Now when you do a get()call on a multimap, it gives you a collection. The first item will be your b and the second one will be your c.

现在,当您在多地图上进行get()调用时,它会为您提供一个集合。第一项将是您的 b,第二项将是您的 c。

回答by Ronit

You can also use a key and object to store multiple values in Multimap. Something like this, MultiValueMap mv = new LinkedMultiValueMap<~>();

您还可以使用键和对象在 Multimap 中存储多个值。像这样, MultiValueMap mv = new LinkedMultiValueMap<~>();