0

Currently in my game, if I want to find a player by their unique id I do something like this:

public Player getPlayerByID (int id) {
    for (Player player : playerList) {
        if (player.getId() == id)
            return player;
    }
    return null;
}

Would using a java HashMap be faster for indexing a player? Using something like this:

HashMap<Integer, Player> playerMap = new HashMap<Integer, Player>();

//Some method to fill hash map with players...

//Get player method
public Player getPlayerByID (int id) {
    playerMap.get(id);
}
vedi0boy
  • 1,030
  • 4
  • 11
  • 32
  • 2
    yes it would. [check this for further reference](http://stackoverflow.com/questions/9702087/java-performance-map-vs-list). – SomeJavaGuy Nov 03 '15 at 15:19
  • 1
    @KevinEsche I'd actually say your link is a good duplicate candidate. – Mena Nov 03 '15 at 15:20
  • Oh, I couldn't find that one, I think I was searching with the wrong keywords. Thanks for your help! – vedi0boy Nov 03 '15 at 15:22

0 Answers0