[英]Iterating MultiMap in Java
I have a multimap like below:我有一个如下所示的多图:
map = {config1=[abc, xyz], config2=[abc]}
Now I try to iterate it using the following piece of code现在我尝试使用以下代码对其进行迭代
Iterator<Entry<String, Object>> itr = map.entrySet().iterator();
while(itr.hasNext()) {
Entry<String, Object> entry = itr.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
I am getting the output as:我得到 output 为:
Key = config1, Value = [abc, xyz]
Key = config2, Value = [abc]
but I need the output as:但我需要 output 为:
Key = config1
Value = abc
Value = xyz
Key = config2
Value = abc
How can I achieve this?我怎样才能做到这一点?
I can suggest something like this我可以建议这样的事情
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
String[] arr1 = {"abc", "xyz"};
String[] arr2 = {"abc"};
map.put("config1", arr1);
map.put("config2", arr2);
Iterator<Map.Entry<String, Object>> itr = map.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry<String, Object> entry = itr.next();
System.out.println("entry.getKey() = " + entry.getKey());
for (Object object: (Object[]) entry.getValue()) {
System.out.println("object = " + object);
}
}
}
Then output will look like然后 output 看起来像
entry.getKey() = config2
object = abc
entry.getKey() = config1
object = abc
object = xyz
The key action this is casting String[]
to Object[]
.关键操作是将String[]
转换为Object[]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.