javalinkedhashmap

Get the previous key in a LinkedHashMap?


I have used LinkedHashMap<String, Integer> lhmNotFinal because it is important the order in which keys entered in the map. I get the value of my LinkedHashMap using this:

for (String key:lhmNotFinal.keySet())
{
    System.out.println(key);
}

Now, I want to get the previous value of my key, how can I do that?


Solution

  • It's not a problem related with a LinkedHashMap in general - it's a coding problem. You can do several things:

    String tmp= null;
    
    for (String key : lhmNotFinal.keySet()) {
    
        tmp = key ;    // after the first iteration you have your last key in tmp
    }