0

I want to iterate through a hashmap. I know I can easily use the entrySet. But the problem is I want to access two elements at once.

example:

HasHMap<Integer,Point> myMap = new HashMap<Integer,Point>();
          //I add some points to the map where integer is the id of that point

I want to be able to access two elements at once so I can use the Graphics drawLine method.

I'm not sure if there's a way.

NOTE: I'm using a hashmap because it is easy to find any point by its id as my map has its polygons made from list of ids.

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
Moe
  • 31
  • 1
  • 1
  • 7

1 Answers1

0

Just have a separate Map.Entry variable that you set inside the loop. Then you can use drawTo using this variable and the loop variable.

Map.Entry<?, ?> oldValue = null;
for(Map.Entry<?, ?> newValue : values) {
   if (oldValue != null) {
      doSomethingWithBoth(oldValue, newValue);
   }
   oldValue = newValue;
}

I dont' know what kind of variables you are using thus the ?.... You should put the type you are using there.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • This works perfectly. although it took me a bit to understand its logic. Thanks. Great logic behind this work. – Moe Nov 22 '14 at 05:58