Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

(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!

added 167 characters in body
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93

edit: Once again, @Misha 's answer is a welcome improvement of mine, do take a look at that too!

edit: Once again, @Misha 's answer is a welcome improvement of mine, do take a look at that too!

added 317 characters in body
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93
public int rarest(Map<String, Integer> m)throws Exception
  1. Count the number of occurrences for each age

    • You can apply a groupingBy() on m's values, using Collectors.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()))
      
  2. 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
  1. Count the number of occurrences for each age

    • You can apply a groupingBy() on m's values, using Collectors.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()))
      
  2. 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
  1. Count the number of occurrences for each age

    • You can apply a groupingBy() on m's values, using Collectors.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()))
      
  2. 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();
}
added 58 characters in body
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93
Loading
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /