I don't mean to sound condescending, but be logical. Maps are used for when you need key -> value mappings; for instance, if you wanted to map a weapon to some sort of poison type.
Code:Map<Integer, PoisonType> map = new HashMap<>(); // Here we map an Integer value of "4151" to the PoisonType "MILD" map.put(4151, PoisonType.MILD); // Now we retrieve the value that we just mapped, this will obviously print "MILD" System.out.println(map.get(4151));
Do you need to map anything in this situation? No. Therefore, in this context a Map should not be used. The best alternative in my opinion would be a HashSet (which ironically is backed by a HashMap) because with a proper hashcode and equals implementation this collection will probably yield the best performance. Set's also do not allow duplicates, which in this case is perfect (you wouldn't want your minigame to have duplicate instances of the same player, would you?).
Code:static Set<Player> players = new HashSet<>(); void join(Player p) { if(players.add(p)) { p.move(new Position(...)); p.sendMessage("You enter the minigame!"); players.forEach(plr -> plr.sendMessage(p.username+" has just joined the minigame!")); } }
Or if you know nothing about hashcode and it's agreement with equals, and how that pertains to the performance of hash based collections then a simple LinkedList will do. Although you'll have to check if the list already contains the player.
Code:static List<Player> players = new LinkedList<>(); void join(Player p) { if(players.contains(p)) return; players.add(p); p.move(new Position(...)); p.sendMessage("You enter the minigame!"); players.forEach(plr -> plr.sendMessage(p.username+" has just joined the minigame!")); }
You may also be interested in giving [Only registered and activated users can see links. ] a little read if you want to learn a lot more.


