|
| 1 | +package com.baeldung.map.util; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Comparator; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Map.Entry; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +public class MapMax { |
| 11 | + |
| 12 | + public <K, V extends Comparable<V>> V maxUsingIteration(Map<K, V> map) { |
| 13 | + |
| 14 | + Map.Entry<K, V> maxEntry = null; |
| 15 | + |
| 16 | + for (Map.Entry<K, V> entry : map.entrySet()) { |
| 17 | + |
| 18 | + if (maxEntry == null || entry.getValue() |
| 19 | + .compareTo(maxEntry.getValue()) > 0) { |
| 20 | + maxEntry = entry; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + return maxEntry.getValue(); |
| 25 | + } |
| 26 | + |
| 27 | + public <K, V extends Comparable<V>> V maxUsingCollectionsMax(Map<K, V> map) { |
| 28 | + |
| 29 | + Entry<K, V> maxEntry = Collections.max(map.entrySet(), new Comparator<Entry<K, V>>() { |
| 30 | + public int compare(Entry<K, V> e1, Entry<K, V> e2) { |
| 31 | + return e1.getValue() |
| 32 | + .compareTo(e2.getValue()); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + return maxEntry.getValue(); |
| 37 | + } |
| 38 | + |
| 39 | + public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndLambda(Map<K, V> map) { |
| 40 | + |
| 41 | + Entry<K, V> maxEntry = Collections.max(map.entrySet(), (Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue() |
| 42 | + .compareTo(e2.getValue())); |
| 43 | + |
| 44 | + return maxEntry.getValue(); |
| 45 | + } |
| 46 | + |
| 47 | + public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndMethodReference(Map<K, V> map) { |
| 48 | + |
| 49 | + Entry<K, V> maxEntry = Collections.max(map.entrySet(), Comparator.comparing(Map.Entry::getValue)); |
| 50 | + |
| 51 | + return maxEntry.getValue(); |
| 52 | + } |
| 53 | + |
| 54 | + public <K, V extends Comparable<V>> V maxUsingStreamAndLambda(Map<K, V> map) { |
| 55 | + |
| 56 | + Optional<Entry<K, V>> maxEntry = map.entrySet() |
| 57 | + .stream() |
| 58 | + .max((Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue() |
| 59 | + .compareTo(e2.getValue())); |
| 60 | + |
| 61 | + return maxEntry.get() |
| 62 | + .getValue(); |
| 63 | + } |
| 64 | + |
| 65 | + public <K, V extends Comparable<V>> V maxUsingStreamAndMethodReference(Map<K, V> map) { |
| 66 | + |
| 67 | + Optional<Entry<K, V>> maxEntry = map.entrySet() |
| 68 | + .stream() |
| 69 | + .max(Comparator.comparing(Map.Entry::getValue)); |
| 70 | + |
| 71 | + return maxEntry.get() |
| 72 | + .getValue(); |
| 73 | + } |
| 74 | + |
| 75 | + public static void main(String[] args) { |
| 76 | + |
| 77 | + Map<Integer, Integer> map = new HashMap<Integer, Integer>(); |
| 78 | + |
| 79 | + map.put(1, 3); |
| 80 | + map.put(2, 4); |
| 81 | + map.put(3, 5); |
| 82 | + map.put(4, 6); |
| 83 | + map.put(5, 7); |
| 84 | + |
| 85 | + MapMax mapMax = new MapMax(); |
| 86 | + |
| 87 | + System.out.println(mapMax.maxUsingIteration(map)); |
| 88 | + System.out.println(mapMax.maxUsingCollectionsMax(map)); |
| 89 | + System.out.println(mapMax.maxUsingCollectionsMaxAndLambda(map)); |
| 90 | + System.out.println(mapMax.maxUsingCollectionsMaxAndMethodReference(map)); |
| 91 | + System.out.println(mapMax.maxUsingStreamAndLambda(map)); |
| 92 | + System.out.println(mapMax.maxUsingStreamAndMethodReference(map)); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments