(the following part is heavily inspired heavily inspired from my other answer to a recent, similar question)
edit: Once again, @Misha @Misha's answer is a welcome improvement of mine, do take a look at that too!
(the following part is heavily inspired from my other answer to a recent, similar question)
edit: Once again, @Misha's answer is a welcome improvement of mine, do take a look at that too!
(the following part is heavily inspired from my other answer to a recent, similar question)
edit: Once again, @Misha's answer is a welcome improvement of mine, do take a look at that too!
public int rarest(Map<String, Integer> m)throws Exception
Count the number of occurrences for each age
You can apply a
groupingBy()
onm
's values, usingCollectors.counting()
to populate the intermediate map's values:// assume map is the method argument Map<Integer, Long> temp = map.entrySet().stream() .collect(Collectors.groupingBy(Entry::getValue, Collectors.counting()))
Sort by the least values, then by the youngest age
From this intermediate map, you can stream the entries, sort by the values using a custom
Entry.comparingByValue()
Comparator
lambda declaration and then return the first entry, i.e. the rarest:temp.entrySet().stream() .sorted(Entry(e1, e2) -> { int v = e1.comparingByValuegetValue().compareTo(e2.getValue()); return v != 0 ? v : e1.getKey().compareTo(e2.getKey()); }) .findFirst().map(Entry::getKey).get().intValue();
public static int rarest(Map<String, Integer> map) {
return map.entrySet().stream()
.collect(Collectors.groupingBy(Entry::getValue,
Collectors.counting()))
.entrySet().stream()
.sorted(Entry(e1, e2) -> {
int v = e1.comparingByValuegetValue().compareTo(e2.getValue());
return v != 0 ? v : e1.getKey().compareTo(e2.getKey());
})
.findFirst().map(Entry::getKey).get().intValue();
}
public int rarest(Map<String, Integer> m)throws Exception
Count the number of occurrences for each age
You can apply a
groupingBy()
onm
's values, usingCollectors.counting()
to populate the intermediate map's values:// assume map is the method argument Map<Integer, Long> temp = map.entrySet().stream() .collect(Collectors.groupingBy(Entry::getValue, Collectors.counting()))
Sort by the least values, then by the youngest age
From this intermediate map, you can stream the entries, sort by the values using
Entry.comparingByValue()
and then return the first entry, i.e. the rarest:temp.entrySet().stream() .sorted(Entry.comparingByValue()) .findFirst().map(Entry::getKey).get().intValue();
public static int rarest(Map<String, Integer> map) {
return map.entrySet().stream()
.collect(Collectors.groupingBy(Entry::getValue,
Collectors.counting()))
.entrySet().stream()
.sorted(Entry.comparingByValue())
.findFirst().map(Entry::getKey).get().intValue();
}
public int rarest(Map<String, Integer> m)throws Exception
Count the number of occurrences for each age
You can apply a
groupingBy()
onm
's values, usingCollectors.counting()
to populate the intermediate map's values:// assume map is the method argument Map<Integer, Long> temp = map.entrySet().stream() .collect(Collectors.groupingBy(Entry::getValue, Collectors.counting()))
Sort by the least values, then by the youngest age
From this intermediate map, you can stream the entries, sort by the values using a custom
Comparator
lambda declaration and then return the first entry, i.e. the rarest:temp.entrySet().stream() .sorted((e1, e2) -> { int v = e1.getValue().compareTo(e2.getValue()); return v != 0 ? v : e1.getKey().compareTo(e2.getKey()); }) .findFirst().map(Entry::getKey).get().intValue();
public static int rarest(Map<String, Integer> map) {
return map.entrySet().stream()
.collect(Collectors.groupingBy(Entry::getValue,
Collectors.counting()))
.entrySet().stream()
.sorted((e1, e2) -> {
int v = e1.getValue().compareTo(e2.getValue());
return v != 0 ? v : e1.getKey().compareTo(e2.getKey());
})
.findFirst().map(Entry::getKey).get().intValue();
}