简体   繁体   English

Java使用SimpleEntry在地图上进行迭代

[英]Java Iterate Over a Map with SimpleEntry

I have a TreeMap with 3 entries, of the following type: 我有一个具有以下类型的3个条目的TreeMap:

Map<String, Entry<String, String>> treeMap = new TreeMap<String, Entry<String, String>>();

I populate the map with the following code: 我使用以下代码填充地图:

SimpleEntry<String, String> simpleEntry = new SimpleEntry<String, String>("", "");

for (int i = 0; i < listSites.size(); i++) {

        String stringkey = listSites.get(i);
        String stringValue = listSitesDesc.get(i);
        String stringLink = listSitesLinks.get(i);

        entry = new SimpleEntry<String, String>(stringValue, stringLink);
        actionMap.put(stringkey, simpleEntry);

}

This works just as expected. 正如预期的那样。

My issue is how to retrieve the individual values form the sorted TreeMap. 我的问题是如何从排序的TreeMap中检索单个值。

EDITED : 编辑:

for (Map.Entry<String, Entry<String, String>> entrySet : treeMap.entrySet()) {

        String key = entrySet.getKey();
        String valueKey = simpleEntry.getKey();
        String valueValue = simpleEntry.getValue();
}

This prints the correct values for the key, like: 这将为密钥输出正确的值,例如:

 key = one , two , three , four , five ..... 

But I am getting the wrong values for valueKey and valueValue, like: 但是我得到了valueKey和valueValue的错误值,例如:

valueKey = bear , bear , bear , bear , bear ...

instead of : 代替 :

valueKey = dog , cat , monkey, tiger , bear ....

What am I getting wrong? 我怎么了?

Map.keySet() (unsurprisingly) returns the keys , not the values . Map.keySet() (毫不奇怪)返回 ,而不是value

To get the values , use (again, unsurprisingly) Map.values() . 要获取 ,请使用(同样,毫不奇怪) Map.values()

To iterate over all the key/value pairs, ie the entries : 要遍历所有键/值对,即条目

for (Map.Entry<String, Entry<String, String>> entry : treeMap.entrySet()) {
    // showing how to get at the parts:
    String key = entry.getKey();
    String valueKey = entry.getValue().getKey();
    String valueValue = entry.getValue().getValue();

}

This is an unusual way to approach whatever problem you're trying to solve, because each key has only one value, so you have "key -> key -> value", which is the same as just "keykey -> value". 这是解决您要解决的任何问题的一种不寻常的方法,因为每个键只有一个值,所以您拥有“键->键->值”,与“键->值”相同。 Consider dispensing with the outer layer map and using just Map<String, String> , and/or using classes with (named) fields to store your data. 考虑放弃外层地图,仅使用Map<String, String>和/或使用带有(命名)字段的类来存储数据。

First, take @Bohemian's answer, that's what does what you want. 首先,以@Bohemian的答案为准,这就是您想要的。 and any future googles. 以及将来的所有Google。 It's not working because you have a bug :D (which is why we all come to stack overflow, depending on your definition of bug) 它不起作用是因为您有一个bug:D(这就是为什么我们所有人都会出现堆栈溢出的原因,这取决于您对bug的定义)

SimpleEntry simpleEntry = new SimpleEntry("", ""); SimpleEntry simpleEntry =新的SimpleEntry(“”,“”);

... ...

actionMap.put(stringkey, simpleEntry entry ); actionMap.put(stringkey, simpleEntry entry );

does that help? 有帮助吗?

I don't understand your question... 我不明白你的问题...

But how do I get the second and third values of the map, those defined by the SimpleEntry? 但是,如何获取地图的第二个和第三个值,即SimpleEntry定义的那些值?

Since it's not clear exactly what you're looking for (which is probably why you weren't able to find an answer) here are a few (but not all) ways to iterate over a map: 由于不清楚您要寻找的是什么(这可能就是为什么您找不到答案的原因),这里有几种(但不是全部)迭代地图的方法:

// I'm changing your map a little to avoid confusion over the word 'Entry'.
Map<String, SimpleEntry<String, String>> treeMap = new TreeMap<>();
for (Map.Entry<String, SimpleEntry<String, String>> ent: treeMap.entrySet())
{
  String key = ent.getKey();
  SimpleEntry val - ent.getValue();
}

If you're using Java 8 or later... 如果您使用的是Java 8或更高版本...

// Using your original map
Map<String, Entry<String, String>> treeMap = new TreeMap<>();
treeMap.forEach((key, value) ->
{
  // key is a String, value is an Entry<String, String>>
});

// Explicit parameter types can increase code readability
treeMap.forEach((String key, Entry<String, String>> value) ->
{
  // Have fun with the contents of your map!
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM