Java 如何遍历谷歌多地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3934470/
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
How to iterate through google multimap
提问by Amit Kumar Gupta
I have to iterate through google multimap. But
我必须遍历谷歌多地图。但
- I am using jdk 1.4 and can't switch to higher version. So i can not use generic features.
- My multimap can have multiple values for a key.
- There might be a situation when a value of multimap is multimap in itself
- 我使用的是 jdk 1.4,无法切换到更高版本。所以我不能使用通用功能。
- 我的 multimap 可以有多个键值。
- 可能会出现 multimap 的值本身就是 multimap 的情况
采纳答案by ColinD
Google Collections (now Guava) is a Java 1.5 library... even ignoring the lack of generics in Java 1.4, it likely uses things that were added in 1.5, making it incompatible. That said, there are various ways to iterate through a Multimap
.
Google Collections(现在的 Guava)是一个 Java 1.5 库……即使忽略 Java 1.4 中缺乏泛型,它也可能使用 1.5 中添加的东西,使其不兼容。也就是说,有多种方法可以遍历Multimap
.
By key, collection pairs in Java8:
通过键,Java8 中的集合对:
multimap.asMap().forEach((key, collection) -> {...});
Iterate through all values:
遍历所有值:
for (Object value : multimap.values()) { ... }
Iterate through all keys (a key that maps to multiple values coming up multiple times in the iteration):
遍历所有键(映射到迭代中多次出现的多个值的键):
for (Object key : multimap.keys()) { ... }
Iterate through the key set:
遍历密钥集:
for (Object key : multimap.keySet()) { ... }
Iterate through the entries:
遍历条目:
for (Map.Entry entry : multimap.entries()) { ... }
Iterate through the value Collection
s:
遍历值Collection
s:
for (Collection collection : multimap.asMap().values()) { ... }
You can also get the corresponding Collection
for each key in the keySet()
using get
as described by bwawok.
您还可以按照bwawok 的描述获取使用中Collection
每个键的对应值。keySet()
get
Edit:I didn't think about the fact that Java 1.4 didn't have the foreach loop either, so of course each loop above would have to be written using the Iterator
s directly.
编辑:我没有想到 Java 1.4 也没有 foreach 循环,所以当然上面的每个循环都必须Iterator
直接使用s编写。
回答by bwawok
I am on Java 6, but this should be pretty close... sorry if I missed something java 1.4ish
我在 Java 6 上,但这应该非常接近......对不起,如果我错过了 Java 1.4ish
Set keySet = listmultimap.keySet();
Iterator keyIterator = keySet.iterator();
while (keyIterator.hasNext() ) {
String key = (String) keyIterator.next();
List values = listmultimap.get( key );
}
Each get will get you everything back that matched that key. Then you can either peel those off, or do whatever you want with them.
每次获取都会让您返回与该键匹配的所有内容。然后你可以剥掉它们,或者对它们做任何你想做的事情。
回答by Jana
When you use guava multimap, the values are stored in Collection, not List
使用 guava multimap 时,值存储在 Collection 中,而不是 List
Set<String> keys = multiMap.keySet();
for (String keyprint : keys) {
System.out.println("Key = " + keyprint);
Collection<String> values = multiMap.get(keyprint);
for(String value : values){
System.out.println("Value= "+ value);
}
}
回答by Kolibril
A minimal example would be:
一个最小的例子是:
public class Test {
public static void main(String[] args) {
ListMultimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("hello", " name");
multimap.put("hello", " name2");
multimap.put("world", " ocean");
for (String firstName : multimap.keySet()) {
List<String> lastNames = multimap.get(firstName);
System.out.println(firstName + " " + lastNames);
}
}
}
and the output:
和输出:
world [ ocean]
hello [ name, name2]