HashMap help

Discussion in 'Spigot Plugin Development' started by Creepermanthe3rd, Jun 12, 2017.

  1. Hello, I was wondering how I would go about listing the 5 highest values from a hashmap,
    here is my hashmap.
    Code (Text):

    HashMap<Player, Double>damage = new HashMap<Player, Double>();
     
     
  2. Additionally you can use Java 8 streams as well.
    Code (Text):
    List<Double> topFiveValues = new ArrayList<>();
         
            topFiveValues = damage.entrySet()
                    .stream()
                    .sorted(Collections.reverseOrder((e1,e2) -> Double.compare(e1.getValue(), e2.getValue())))
                    .map(e -> e.getValue()).limit(5)
                    .collect(Collectors.toList());
     
    • Agree Agree x 1
  3. This seems to be working fine but, how would I get the player for ex the 1st largest amount?
     
  4. Well with that example it is only values since i mapped only value. You may remove it and you will have a list of Entry and you may call getKey() for the player.
     
    • Agree Agree x 1