HashMap

Discussion in 'Plugin Development' started by AyWuzzUp, Nov 28, 2017.

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

    AyWuzzUp

    I currently have a HashMap that stores String and Integer and it sorts the player's depending on their kills so:

    Code:
    Player1 100
    Player2 35
    Player3 21
    Player4 15
    Player5 12
    Player7 9
    
    How would I be able to get the top player (Player1)
     
  2. Offline

    Zombie_Striker

    @AyWuzzUp
    1. Create a String variable. This stores the player with the most points/kills
    2. Create an int. This will be the amount of kills for that players. Set it to -1.
    3. For loop through all of the entries.
    4. If the kills for that value is greater than the int from #2, set the String to be equal to the player and the int to be equal to the amount of kills.
    5. Once the for loop is over, String will be equal to the player with the most kills.
     
  3. Offline

    AyWuzzUp

    This is what I have so far and it works fine,

    Code:
    File dir = new File(plugin.getDataFolder().getPath());
    File[] directoryListing = dir.listFiles();
    if (directoryListing != null) {
    for (File child : directoryListing) {
    userFile = YamlConfiguration.loadConfiguration(child);
    kills.put(userFile.getString("User.CurrentName"), userFile.getInt("User.Kills"));
    }
    
    kills.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
    .limit(5)
    .forEach(e -> {
    player.sendMessage(e.getKey() + " " + e.getValue());
    });
    
    is there any way I can simply make a method or something where I can do retrieve(1) this will return the top player's name and int?
     
    Last edited: Nov 28, 2017
Thread Status:
Not open for further replies.

Share This Page