Double Iteration Over a Hashmap
I'm using a hashmap to hold multiple Player objects for a multi-player game. In one section of my code, I need to check the distances from each player to every other player. I learned how to iterate over a hashmap, but things seem to go very wrong when I try two levels of nested iteration over the same map.
Code:
Iterator a = player.entrySet().iterator();
while (a.hasNext())
{
Map.Entry aMe = (Map.Entry)a.next();
Player myAPlayer = (Player) aMe.getValue();
Iterator b = player.entrySet().iterator();
while (b.hasNext())
{
Map.Entry bMe = (Map.Entry)b.next();
Player myBPlayer = (Player) bMe.getValue();
// First make sure that myAPlayer and myBPlayer are not the same,
// then do something here with both players.
}
}
I am assuming what I am doing here is incorrect. What would be the simplest method to perform a two level nested iteration on a hashmap?
Thank you in advance for your time.