simplify with comparingByValue()
This commit is contained in:
parent
027cbaf9f9
commit
1ad0c04839
@ -1,7 +1,6 @@
|
||||
package com.baeldung.map.mapmax;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
@ -26,12 +25,7 @@ public class MapMax {
|
||||
|
||||
public <K, V extends Comparable<V>> V maxUsingCollectionsMax(Map<K, V> map) {
|
||||
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), new Comparator<Entry<K, V>>() {
|
||||
public int compare(Entry<K, V> e1, Entry<K, V> e2) {
|
||||
return e1.getValue()
|
||||
.compareTo(e2.getValue());
|
||||
}
|
||||
});
|
||||
Entry<K, V> maxEntry = Collections.max(map.entrySet(), Entry.comparingByValue());
|
||||
|
||||
return maxEntry.getValue();
|
||||
}
|
||||
@ -72,7 +66,7 @@ public class MapMax {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
|
||||
map.put(1, 3);
|
||||
map.put(2, 4);
|
||||
|
Loading…
x
Reference in New Issue
Block a user