Finding the keys of map with the highest values (equality may occur) I am currently implementing a Poker game, I have created all the logic for getting the rank of a hand, and I now have to determine the winner of a given hand.

Due to this being Poker, there may be multiple winners in a round. I, therefore, need to find all the keys (Player) that have the best hand (regardless of how many there are).

   public Map&lt;Player, Hand&gt; determineWinner(Map&lt;Player, Hand&gt; hands) {
        // Initialize worst possible hand (any hand beats this) - less verbose than
        // having to get a random element form the map
        Hand currentBestHand = new Hand(RankType.HIGH_CARD, CardRank.DEFAULT, new ArrayList&lt;&gt;(), new ArrayList&lt;&gt;());
        Map&lt;Player, Hand&gt; bestHands = new HashMap&lt;&gt;();
        for (Map.Entry&lt;Player, Hand&gt; playerAndHand : hands.entrySet()) {
            int comparisonValue = playerAndHand.getValue().compareTo(currentBestHand);
            if (comparisonValue &gt; 0) {
                currentBestHand = playerAndHand.getValue();
                bestHands.clear();
                bestHands.put(playerAndHand.getKey(), playerAndHand.getValue());
            } else if (comparisonValue == 0) {
                bestHands.put(playerAndHand.getKey(), playerAndHand.getValue());
            }
        }
        return bestHands;
    }


However, I am using Java 8 and thus a lot of streams. I couldn't figure out a (pretty) way to implementing the above in streams, and therefore ended up coding it in a loop. I welcome any suggestions, especially those related to writing it succinctly and efficiently using Java 8 streams.
To use streams you'd have to make the score part of the object (at least on an intermediate level) so that you can find the maximum based on the score.

Here's a little example based on a string/string map and String.length() as score replacement (as I don't have your original objects). Should be sufficient to illustrate the idea:

    Map&lt;String, String&gt; m = ...

    Map&lt;Integer, List&lt;Entry&lt;String, String&gt;&gt;&gt; intermediateByScore = m.entrySet().stream()
        .collect(Collectors.groupingBy(e -&gt; e.getValue().length()));

    Entry&lt;Integer, List&lt;Entry&lt;String, String&gt;&gt;&gt; highest = intermediateByScore.entrySet().stream()
        .sorted((e1, e2) -&gt; e2.getKey().compareTo(e1.getKey()))
        .findFirst()
        .get();

    Map&lt;String, String&gt; result = highest.getValue().stream()
        .collect(Collectors.toMap(e -&gt; e.getKey(), e -&gt; e.getValue()));


... or, after a few inlines of intermediate variables:

    Map&lt;String, String&gt; result = m.entrySet().stream()
        .collect(Collectors.groupingBy(e -&gt; e.getValue().length()))
        .entrySet().stream()
        .sorted((e1, e2) -&gt; e2.getKey().compareTo(e1.getKey()))
        .findFirst()
        .get().getValue().stream()
        .collect(Collectors.toMap(e -&gt; e.getKey(), e -&gt; e.getValue()));


Is this better than the loop: I don't personally think so. My advice: stick to your loop or revise your data structures to better support this approach.