java - Java 中的多值映射

标签 java collections hashmap

我正在研究具有多个参数(1 个键,2 个值)的 Hashmap 我能够为我的问题找到 apache 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]

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

最佳答案

您可以尝试以下操作:

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);
} 

关于java - Java 中的多值映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704568/

相关文章:

java - 使用 Android 注释绑定(bind)到现有服务?

Java:从缓冲图像中获取 RGBA 作为整数数组

Java解析嵌套conf文件

java - 如何根据两个列表包含的元素比较两个列表?

用于唯一对象实例的 Java Map?

java - 当我尝试在 HashMap 中输入新 key 时,如何计算比较次数?

java,使用字典进行zip?

java - keyset 和 key 不同的数据类型

java - 为什么我不能在列表上使用 Apache 的 StringUtils.join?

java - 如何从 Java 中的 map 列表中获取所有值?