Util How to get top 10 in a Hashmap

Discussion in 'Resources' started by mrgreen33gamer, Nov 30, 2014.

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

    mrgreen33gamer

    Hi there!

    If you have a custom coded Economy system and want to get the highest credits that players have, instance a top 10 best players with the most credits, then this is the topic for you!

    I bring you not a lib, but a simple piece of code that can be used in a command!

    WARNING: If you do not have at least more than 10 values in your HashMap, errors will occur!

    Code:java
    1. if(commandLabel.equalsIgnoreCase("topplayer")){
    2. if(sender instanceof Player){
    3. Player player = (Player)sender;
    4. player.sendMessage(ChatColor.GOLD + "===== Top 10 players ===== ");
    5.  
    6.  
    7. class ValueComparator implements Comparator<String> {
    8.  
    9. Map<String, Integer> base;
    10. public ValueComparator(HashMap<String, Integer> map) {
    11. this.base = map;
    12. }
    13.  
    14. public int compare(String a, String b) {
    15. if (base.get(a) >= base.get(b)) {
    16. return -1;
    17. } else {
    18. return 1;
    19. }
    20. }
    21. }
    22.  
    23.  
    24. ValueComparator bvc = new ValueComparator(yourmap);
    25. TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);
    26. sorted_map.putAll(yourmap);
    27.  
    28. for(int i = 1; i < 11; i ++){
    29. Entry<String, Integer> e = sorted_map.pollFirstEntry();
    30. String pname = e.getKey();
    31. int score = e.getValue();
    32. player.sendMessage(i + ". " + ChatColor.GOLD + pname + ": " + ChatColor.WHITE + score);
    33. }
    34.  
    35. }
    36. }



    Code:java
    1. class ValueComparator implements Comparator<String> {
    2.  
    3. Map<String, Integer> base;
    4. public ValueComparator(HashMap<String, Integer> map) {
    5. this.base = map;
    6. }
    7.  
    8. public int compare(String a, String b) {
    9. if (base.get(a) >= base.get(b)) {
    10. return -1;
    11. } else {
    12. return 1;
    13. }
    14. }
    15. }


    This sorts the map that you specify!

    Code:java
    1. ValueComparator bvc = new ValueComparator(yourmap);
    2. TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);
    3. sorted_map.putAll(yourmap);

    ValueComparator sorts your map and the TreeMap stores the sorted data!

    Code:java
    1. for(int i = 1; i < 11; i ++){
    2. Entry<String, Integer> e = sorted_map.pollFirstEntry();
    3. String pname = e.getKey();
    4. int score = e.getValue();
    5. player.sendMessage(i + ". " + ChatColor.GOLD + pname + ": " + ChatColor.WHITE + score);
    6. }


    Here we finally get the top 10! If you want change the top players, just change the 11 to something like 4 to get the top 3!

    Examples:

    Top 5 =
    Code:
    for(int i = 1; i < 6; i ++){
    Top 20 =
    Code:
    for(int i = 1; i < 21; i ++){
    Top 3 =
    Code:
    for(int i = 1; i < 4; i ++){

    Please hit the LIKE button if this supported you in ANY way or if you think this is a USEFUL topic! This helped me out a plenty!

    ~mrgreen33gamer
     
    xXBeLkAXx, ChipDev and Yekllurt like this.
  2. Skionz, Skyost, ChipDev and 1 other person like this.
  3. Offline

    Yekllurt

    mrgreen33gamer This tutorial is ok i woud recommend u to explain more.
    I just tried it in a Hello World programm with 8 Values an it works out problem
     
  4. Offline

    xTrollxDudex

    The like farming is a tad bit too apparent in this one
     
  5. Offline

    ChipDev

    Did you just copy paste this from stackoverflow/ someone who pasted it from stack overflow?, Just get the score, save that to a list, then get the player, set that into the list, sort the scores, then reverse them
     
  6. Offline

    user_90854156

    Phasesaber likes this.
  7. Offline

    mrgreen33gamer

  8. Offline

    Gater12

    mrgreen33gamer
    The thread you posted for help itself can help people already. No need to make a tutorial about it.
     
    teej107, xTrollxDudex and Skionz like this.
  9. Offline

    xTrollxDudex

    Yes, that is a problem
     
    Phasesaber, Avygeil and AdamQpzm like this.
  10. Offline

    [b]Blake[/b]

    Is this really a UTIL?
     
  11. Offline

    turt2live

    Why would you reverse it after? Surely you can figure out how to sort in the correct order the first time... Because, well, that's what sorting is: to put things in order.
     
    teej107 likes this.
Thread Status:
Not open for further replies.

Share This Page