rainer
rainer

Reputation: 3411

Java Iterate Over a Map with SimpleEntry

I have a TreeMap with 3 entries, of the following type:

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.

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 = bear , bear , bear , bear , bear ...

instead of :

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

What am I getting wrong?

Upvotes: 0

Views: 511

Answers (3)

DefyGravity
DefyGravity

Reputation: 6011

First, take @Bohemian's answer, that's what does what you want. and any future googles. 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)

SimpleEntry simpleEntry = new SimpleEntry("", "");

...

actionMap.put(stringkey, simpleEntry entry);

does that help?

Upvotes: 0

Paul
Paul

Reputation: 20061

I don't understand your question...

But how do I get the second and third values of the map, those defined by the 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...

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

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

Map.keySet() (unsurprisingly) returns the keys, not the values.

To get the values, use (again, unsurprisingly) 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.

Upvotes: 4

Related Questions