Solved Get 3 best Integers in Map<String, Integer> (For top)

Discussion in 'Spigot Plugin Development' started by _SebazCRC_, Feb 23, 2018.

Thread Status:
Not open for further replies.
  1. Hello guys, thanks for reading this thread, how i can get the best integer??.


    Code (Text):
    map.put(player.getName(), 5); // PlayerName has 5 kills!
    map.put("Fulanito", 10);
    map.put("Pepito Alcachofa", 25);
  2. You will need to compare every value in the map and keep track of the highest three values with a little simple logic.

    You could also turn it into an array and sort it, but that is much more resource intensive. Only do that for small or infrequent use.
  3. Mas


    You could do this nicely with streams and comparators, if you Google "java sort map with stream" you'll find lots of results.
  4. Which is probably far beyond the complexity someone asking this question is ready for, and grossly inefficient..
    • Optimistic Optimistic x 2
  5. Are you trying to get the highest value and only the highest ? Or order the hash map by top kills ?
  6. Example? i don't understand
  7. Get three highest values in 12 values list
  8. Iterate over the map and check each value, keep the top three values you have encountered. Pretty simple if/else block of a few lines.

    That is the most efficient and performant solution, but for only 12 entries it does not even matter, use a TreeMap
  9. Yeah probably your best bet is just keeping track of the top 3 values using some logic. Since you only care about the top 3, using a Tree data structure would be overkill, but it shouldn't really matter that much unless you're running the plugin on a big server.
  10. I don't speak english xD, you can explain with code, and i don't understand about TreeMap
  11. I don't know but I would go to the easy way.
    When you want to update the top 3 just sort an array of 12 values.
    If we talk in performance an efficient it won't be really a difference from a TreeMap since the amount of values we have to sort aren't that much.

    If we were talking about much more values and need to add/remove and sort very often then I would use another thing but talking about 12 values use an array.
  12. Stop asking people to give you the code all your posts are just you asking for the code. People have told you how to go about doing it and if you don't understand part of it do some research and learn about it.
  13. This is an example for getting the HIGHEST number. You can do the rest:

    Code (Text):
    public Integer getTopValue(){

        int topVal = 0;

        yourHashMap.values().forEach(val -> {
            if(val > topVal){
                topVal = val;
        return topVal;
  14. A simple Google search gives a good stackoverflow as first result ...

    Don’t even use this code if you don’t understand what it does.

    On the internet it is full with guides, examples and people asking similar questions. Try to Google first I guess :)
  15. To get 3 highest?
    Edit: Thanks for help me
  16. Mas


    You realise one of the reasons the stream API was made was to simplify sorting of collections so people aren't having to write their own , long-winded methods to do the same thing? And where you got inefficient from I'm not sure :p
  17. Use a TreeMap instead of a HashMap and just use sort...
  18. Sebas, estaria bien si en vez de pedir que hagas tu trabajo intentaras hacer las cosas tu mismo, entiendo que no sepas Java, pero eso no significa que nosotros te vayamos a hacer el plugin, tendras que aprender igual que lo hemos hecho todos, si no sabes como usar un simple TreeMap deberias empezar a aprender.
  19. "Sebas, it would be nice if instead of asking you to do your work you will try to do things yourself, I understand that you do not know Java, but that does not mean that we will do the plugin, you will have to learn as we have all done, if You do not know how to use a simple TreeMap you should start learning."
Thread Status:
Not open for further replies.