Reputation: 585
String key = Collections.max(countMap.entrySet(), (entry1, entry2) -> entry1.getValue() - entry2.getValue()).getKey();
System.out.println(key);
Set<Entry<String,Integer>> entrySet = countMap.entrySet();
Collections.max(countMap.entrySet());
Here the first line of code "Collections.max(Collection<?extends T>, Comparator<? super T>)
" taking two arguments as Set and comparator, which works fine.
But last line code "Collections.max(countMap.entrySet());
" gives compile time error saying "The method max(Collection) in the type Collections is not applicable for the arguments (Set>)".
Need an explanation for above mention code.
Upvotes: 0
Views: 64
Reputation: 49321
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#max(java.util.Collection) has
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
All elements in the collection must implement the Comparable interface.
Entry
doesn't implement the Comparable interface, so you can't pass it to this method.
The extends
... Comparable
in the constraint on the T
type parameter is what tells the compiler about the requirement.
Upvotes: 5