Skip to content

Commit 183959e

Browse files
author
Kumar Chandrakant
committed
Adding files referenced in the mini article for the Jira item BAEL-2225
1 parent 823ad5e commit 183959e

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.map.util;
2+
3+
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
public class MapMaxUnitTest {
14+
15+
Map<Integer, Integer> map = null;
16+
MapMax mapMax = null;
17+
18+
19+
@Before
20+
public void setupTestData() {
21+
map = new HashMap<Integer, Integer>();
22+
map.put(23, 12);
23+
map.put(46, 24);
24+
map.put(27, 38);
25+
mapMax = new MapMax();
26+
}
27+
28+
@Test
29+
public void givenMap_whenIterated_thenReturnMaxValue() {
30+
assertEquals(new Integer(38), mapMax.maxUsingIteration(map));
31+
}
32+
33+
@Test
34+
public void givenMap_whenUsingCollectionsMax_thenReturnMaxValue() {
35+
assertEquals(new Integer(38), mapMax.maxUsingCollectionsMax(map));
36+
}
37+
38+
@Test
39+
public void givenMap_whenUsingCollectionsMaxAndLambda_thenReturnMaxValue() {
40+
assertEquals(new Integer(38), mapMax.maxUsingCollectionsMaxAndLambda(map));
41+
}
42+
43+
@Test
44+
public void givenMap_whenUsingCollectionsMaxAndMethodReference_thenReturnMaxValue() {
45+
assertEquals(new Integer(38), mapMax.maxUsingCollectionsMaxAndMethodReference(map));
46+
}
47+
48+
@Test
49+
public void givenMap_whenUsingStreamAndLambda_thenReturnMaxValue() {
50+
assertEquals(new Integer(38), mapMax.maxUsingStreamAndLambda(map));
51+
}
52+
53+
@Test
54+
public void givenMap_whenUsingStreamAndMethodReference_thenReturnMaxValue() {
55+
assertEquals(new Integer(38), mapMax.maxUsingStreamAndMethodReference (map));
56+
}
57+
58+
59+
}

0 commit comments

Comments
 (0)