HashMaps - Getting All Players With The Same Key

Discussion in 'Plugin Development' started by iAmReprisal, Aug 13, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    iAmReprisal

    Hello,

    The title pretty much sums it up. I'm trying to get all players that are in a HashMap, and they all have the same key. If this can be done without a for loop, that would be great since it's causing problems for what I'm trying to do.

    Thanks in advance!

    - Reprisal
     
  2. @iAmReprisal

    If you have already obtained all the players in either a list or array, you can just insert it into the HashMap as value. I have tried to come with an object. I have given an example from the PlayerJoinEvent. The last line of code is how you get the data back. You might have to supress the warning by adding
    @SuppressWarnings("unchecked"), but only use this when you are certain, that it is a List<Player>.
    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        HashMap<String, Object> map = new HashMap<>();
    
        List<Player> players = event.getPlayer().getWorld().getPlayers();
    
        map.put("players", players);
        
        List<Player> getPlayers = (List<Player>) map.get("players");
    }
     
    Last edited: Aug 13, 2017
  3. Offline

    iAmReprisal

    @RaxiCax

    I have my HashMap as...
    Code:
    public static HashMap<Player, String> game = new HashMap<>();
    I'm trying to get all the players with a certain string, and I'm trying to do it without a for loop. Since I'm calling the players in a Bukkit Scheduler.
     
  4. @iAmReprisal I don't think that is possible with a HashMap. A HashMap is usually used when you need to target something specific and I don't know exactly what Bukkit Scheduler is (except it is the parent to BukkitTask, I think), but you can loop?

    EDIT: If you have a list, you can use the contains function, however, that is just a loop inside a function..
     
  5. @iAmReprisal
    You can loop through the map like this:
    Code:java
    1. for (Map.Entry<Player, String> entry : game.entrySet()) {
    2. // entry.getValue() will get the String, and entry.getKey() will get the Player
    3. }

    Also, I do not recommend storing Player instances in maps, as it is very easy for this to lead to memory leaks. It is nearly always wiser to store UUIDs instead.
     
  6. Offline

    iAmReprisal

    @RaxiCax
    Code:
    Bukkit.getScheduler()
    That's what I meant :p

    @AlvinB
    How exactly would I check for all players with the same string?
     
  7. @iAmReprisal @AlvinB

    AlvinB's suggestion is not directly your solution you wanted, but it might be the only one you got. You cannot directly obtain how many have this string without counting inside a loop (at least not in Java). I have not tested it, but you can reverse what you are doing and create a HashMap as HashMap<String, List<String>>. The key string being the string you want to search and value list the players UUID string. Then you just have to add players inside the key value.
     
  8. Offline

    OverDodo

    Since its a loop, you can just ask for the String, and if it matches, do something!
     
  9. @RaxiCax
    Well, I think the solution I provided is quite good. If you really wanted to have a fast lookup by String, I would suggest creating a Multimap with the reverse values.
     
  10. @AlvinB In his thread, he says "If this can be done without a for loop, that would be great since it's causing problems for what I'm trying to do.".

    EDIT: I meant the below:
    Code:
    MultiMap<String, HashSet<String>> = new HashMultiMap<>();
    I haven't worked with multimaps before, but I think that's how it is done.
     
  11. @RaxiCax
    Well, no that is not how you use a multimap. A Multimap is basically just a Map but with one key linked to multiple values. If you really want to avoid a for loop, you create a Multimap which is the reverse of the 'game' map which already exists, so it'd be a Multimap<String, Player>. Do however note that I don't think this is necessary and a for loop should be sufficient.
     
Thread Status:
Not open for further replies.

Share This Page