Top 10. How does it work?

Discussion in 'Plugin Development' started by ILoveCode, Nov 23, 2015.

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

    ILoveCode

    Hey guys! I am here because I am curious on how to get the top 10 integers from a config. I have a config that saves all of the players' data and I am trying to get the top 10 scores from there. Any ideas?

    Thanks! Any help is appreciated!
     
  2. Offline

    Zombie_Striker

    Try something like the following:
    1. Make a Hashmap, Integers(the rank) for keys, the score/player for values.
    2. Make a loop, that loops through all the scores in the hashmap.
    3. Once you got a score, loop through all the scores in the hashmap.
    4. If your score is greater than one in the hashmap (either score > hashmapscore || hashmapscore ==null), find all scores that are lower than it, and save it -1 rank from where it is (E.G. Rank 5 has a score of 20. Your score is 21. Rank 5's 20, gets saved as rank 4, rank 4 gets saved as rank 3, ect. and then you set Rank 5 as 21)
    Following these steps, you will get the top ten (this method works for any amount of scores. This can get 100 if you change some of the parameters)
     
  3. Offline

    ILoveCode

    I made the hashmap, looping through it by using
    Code:
    for (Map.Entry<Integer, String> entry : core.top.entrySet()) {
    Is my loop correct?
     
  4. Offline

    Zombie_Striker

    @ILoveCode
    Test it. I was thinking of using just an Integer for loop (for int i = 0....), and loop from 10 to 1 (10 being the highest score, 1 being the lowest). If your way works, use it.
     
  5. Offline

    ILoveCode

  6. Offline

    teej107

    A TreeMap would be more appropriate.
     
    Mrs. bwfctower likes this.
  7. Offline

    ILoveCode

    I have tried using treepmaps but was a bit confused. Do you mind showing me an example?
     
  8. Offline

    DoggyCode™

    Code:
            Map<String, Integer> nonSortedMap = new HashMap<String, Integer>();
            nonSortedMap.put("ape", 1);
            nonSortedMap.put("pig", 3);
            nonSortedMap.put("cow", 1);
            nonSortedMap.put("frog", 2);
    
            for (RowFilter.Entry<String, Integer> entry  : entriesSortedByValues(nonSortedMap)) {
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
            return false;
        }
        static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
            SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
                    new Comparator<Map.Entry<K,V>>() {
                        @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                            int res = e1.getValue().compareTo(e2.getValue());
                            return res != 0 ? res : 1; // Special fix to preserve items with equal values
                        }
                    }
            );
            sortedEntries.addAll(map.entrySet());
            return sortedEntries;
        }
     
  9. Offline

    ILoveCode

    I dont quite understand that.
     
    Last edited: Nov 23, 2015
  10. Offline

    Zombie_Striker

  11. Offline

    DoggyCode™

    Haha, funny enough. But I didn't write it, I grabbed an example from somewhere as he asked.
     
  12. Offline

    ILoveCode

    May I ask where you got that from? Link?
     
  13. Offline

    Zombie_Striker

    Working off of what I already posted....
    Have something like the following
    Code:
    HashMap<> PlayersAndScores;
    Hashmap<Integer,Score> top10scores;
    
    for(The entries from PlayersAndScores){
    ...for(int i = 10; i > 0; i--){
    ......if(top10scores.get(i)/*get the score*/ < the score){
    .........for(int j = 0; j <= i;j++){
    ...........top10scores.put(j-1,toptenscores.get(j));//This puts score at rank 5 at rank 4, rank 4 to rank 3, ect.
    .........}
    .........top10scores.put(i,the score);// puts the score at the open slot.
    .........break;//moves onto next score
    ......}
    ...}
    }
     
  14. Offline

    teej107

    @ILoveCode His example is not the best. A TreeMap is like a HashMap however it sorts the keys unlike a HashMap where you aren't guaranteed order in the keys. If the your Key type for the Map does not implement Comparable, then you will have to implement it yourself or supply a Comparator in the constructor of the TreeMap. Strings and I believe Integer already implements Comparable so you don't need to supply a Comparator if you are using those as a key.
     
  15. Offline

    ILoveCode

    Uh, this is not working and it is really annoying me. I took a look at the javadocs (treemap) and looked up similar threads and tried the pseudocode but nothing :(
     
  16. Offline

    teej107

    @ILoveCode you're lucky I checked this thread. Please tahg me next time. What are you confused about?
     
  17. Offline

    ILoveCode

    What I am mainly confused about is looping through through my HashMap and order the players into a top 10 format
     
  18. Offline

    teej107

    @ILoveCode Again....
    Don't use a HashMap. A TreeMap can take care of the ordering for you.
     
  19. Offline

    ILoveCode

    @teej107, Can you show me how its done? I dont want just flat out code, I would like tutorial comments so I don't just copy and paste. I want to learn something today :)
     
  20. Offline

    Scimiguy

    Then show us what you have tried and we will show you how it is wrong
     
    Zombie_Striker likes this.
  21. Offline

    teej107

    You use it exactly like a regular HashMap however if your key type doesn't implement Comparable, then you would need to supply a Comparator in the constructor when creating the Map. What object type would you be using for your keys?
     
  22. Offline

    ILoveCode

    @teej107 I want the key to be a String (Player name/UUID) and the value to be an integer (Their score)

    I tried what @Zombie_Striker. But then @teej107 suggested to use a TreeMap. I am in need of help.
     
    Last edited by a moderator: Nov 23, 2015
  23. Offline

    Scimiguy

    So where's your code
     
  24. Offline

    teej107

    If you are planning on keeping a single instance of a TreeMap around when scores are changing, then a TreeMap wouldn't be the best solution since a TreeMap has fixed ordering.
    Apart than that, since you want to sort their scores which are the values in the Map, you would have to use a Comparator since TreeMaps sort by keys. The comparator lets you decide the sorting.
     
Thread Status:
Not open for further replies.

Share This Page