Let's say that I had reasons to require a fast lookup of a class instance by multiple value types, for the sake of this explanation I'm going to be using a game server as an example.
Lets say that the server handles users with a static identification number. This number is used to communicate and interact with specific players (Ie: Private Chatting, Trade requests, Combat, Guild invites, and so forth).
This would require the frequent usage of looking the player up by their identification number, based on my current experience the best way to do this is like so: (Please correct me if I'm wrong.)
HashMap<Integer, Player>
However when dealing with networking, there's a lot of times when I'm going to also need to get the player associated with the network session, or "socket" as some people may be more familiar with. Which would look something like so:
HashMap<Connection, Player>
So what I'm trying to figure out is, should I go this route:
HashMap<Integer, Player> playersById;
HashMap<Connection, Player> playersByConnection;
or should I do something a little more "Smashed together" like so:
HashMap<Object[], Player> playersOnline;
and have Object[0]
as the Integer, and Object[1]
as the Connection, then use the one required during lookup.
Or are both of these ways inneficient and incorrect, is there a better/faster way to look them up by either Integer or Connection without duplicating the collections?
Any insight would be GREATLY appreciated.
edit: Also, is there anything against having a HashSet<>
and a HashMap<>
containing the same class references? I've noticed that the HashSet<>
is far more efficient at iterating than the HashMap<>
and have been keeping a Map for lookup and a Set for iterating, is this bad practice?