遍历 Guava Multimaps
Iterating over Guava Mutlimaps
有没有办法在每次迭代中获取 MultiMap 中每个元素的键值对,而无需先将它们添加到列表中?
我想实现这样的东西:
(我知道这行不通)
public class MutliMapTest {
public static void main(String... args) {
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
Iterator it = myMultimap.keySet().iterator();
while(it.hasNext()) {
String type = it.next().toString();
String name = myMultimap.gettype); // This will get me a List, but I want a single element
System.out.println("Iteration : " + type + " : " + name)
}
}
}
我想要一些类似的输出:
Iteration : Fruits : Bannana
Iteration : Fruits : Apple
Iteration : Fruits : Pear
Iteration : Vegetables : Carrot
我认为 entries
方法可以满足您的需求
假设您谈论的是 MultiMap
的 Google Guava 变体
A Multimap
是一个 Map
键 -> 集合,对于这样的 class 在适当的集合上使用嵌套的 foreach 循环:
Multimap<String, String> myMultimap;
for (Map.Entry<String, List<String>> entry : myMultimap.entrySet()) {
for (String value ; entry.getValue()) {
System.out.println(entry.getKey() + ":" + value);
}
}
这似乎是你想要的:
public void test() {
System.out.println("Hello");
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
for (Map.Entry<String, String> e : myMultimap.entries()) {
System.out.println("K : " + e.getKey() + " V : " + e.getValue());
}
}
打印
K : Vegetables V : Carrot
K : Fruits V : Bannana
K : Fruits V : Apple
K : Fruits V : Pear
有没有办法在每次迭代中获取 MultiMap 中每个元素的键值对,而无需先将它们添加到列表中?
我想实现这样的东西:
(我知道这行不通)
public class MutliMapTest {
public static void main(String... args) {
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
Iterator it = myMultimap.keySet().iterator();
while(it.hasNext()) {
String type = it.next().toString();
String name = myMultimap.gettype); // This will get me a List, but I want a single element
System.out.println("Iteration : " + type + " : " + name)
}
}
}
我想要一些类似的输出:
Iteration : Fruits : Bannana
Iteration : Fruits : Apple
Iteration : Fruits : Pear
Iteration : Vegetables : Carrot
我认为 entries
方法可以满足您的需求
假设您谈论的是 MultiMap
的 Google Guava 变体A Multimap
是一个 Map
键 -> 集合,对于这样的 class 在适当的集合上使用嵌套的 foreach 循环:
Multimap<String, String> myMultimap;
for (Map.Entry<String, List<String>> entry : myMultimap.entrySet()) {
for (String value ; entry.getValue()) {
System.out.println(entry.getKey() + ":" + value);
}
}
这似乎是你想要的:
public void test() {
System.out.println("Hello");
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
for (Map.Entry<String, String> e : myMultimap.entries()) {
System.out.println("K : " + e.getKey() + " V : " + e.getValue());
}
}
打印
K : Vegetables V : Carrot
K : Fruits V : Bannana
K : Fruits V : Apple
K : Fruits V : Pear