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

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

Not open for further replies.
1. ### _SebazCRC_

Example:

Code (Text):
map.put(player.getName(), 5); // PlayerName has 5 kills!
map.put("Fulanito", 10);
map.put("Pepito Alcachofa", 25);

2. ### FlyingLlama

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. ### FlyingLlama

Which is probably far beyond the complexity someone asking this question is ready for, and grossly inefficient..

• Optimistic x 2
5. ### Jonnyo101

Are you trying to get the highest value and only the highest ? Or order the hash map by top kills ?

6. ### _SebazCRC_

Example? i don't understand

7. ### _SebazCRC_

Get three highest values in 12 values list

9. ### FlyingLlama

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

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.

11. ### _SebazCRC_

I don't speak english xD, you can explain with code, and i don't understand about TreeMap

12. ### Torciv

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.

13. ### Bobcatsss

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.

https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

14. ### TheWisePotato

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;
}
}
}

15. ### didjee2

A simple Google search gives a good stackoverflow as first result ...

http://bfy.tw/GlJg

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

16. ### _SebazCRC_

To get 3 highest?
Edit: Thanks for help me

17. ### 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

18. ### ExpDev

Use a TreeMap instead of a HashMap and just use sort...

19. ### MaTaMoR_

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.

20. ### ExpDev

"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."