Find the rarest in a map
This is more efficient than the accepted answer.  min is O(1) memory and O(n) time while sorted is O(n) memory and O(n*log(n)) time:

import static java.util.Comparator.comparingLong;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;


Comparator&lt;Map.Entry&lt;Integer, Long&gt;&gt; byRarity = comparingLong(Map.Entry::getValue);
Comparator&lt;Map.Entry&lt;Integer, Long&gt;&gt; byAge = comparingInt(Map.Entry::getKey);

int rarest = data.values().stream()
        .collect(groupingBy(x-&gt;x, counting()))
        .entrySet().stream()
        .min(byRarity.thenComparing(byAge))
        .get()        // or throw if map is empty
        .getKey();