Skip to content

Commit e23f29e

Browse files
committed
changes
1 parent e4dc7f5 commit e23f29e

2 files changed

Lines changed: 47 additions & 63 deletions

File tree

core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,54 @@
55

66
import java.util.AbstractMap;
77
import java.util.HashMap;
8+
import java.util.Iterator;
89
import java.util.LinkedHashMap;
910
import java.util.Map;
11+
import java.util.Set;
1012

11-
import org.junit.Before;
1213
import org.junit.Test;
1314

1415
public class MapFirstPairUnitTest {
1516

16-
private MapFirstPairExample mapFirstPairExample;
17+
private Map.Entry<Integer, String> getFirstPairUsingIterator(Map<Integer, String> map) {
18+
if (map == null || map.size() == 0)
19+
return null;
1720

18-
@Before
19-
public void Setup() {
21+
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet()
22+
.iterator();
2023

21-
mapFirstPairExample = new MapFirstPairExample();
24+
if (iterator.hasNext())
25+
return iterator.next();
26+
27+
return null;
28+
}
29+
30+
private Map.Entry<Integer, String> getFirstPairUsingStream(Map<Integer, String> map) {
31+
if (map == null || map.size() == 0)
32+
return null;
33+
34+
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
35+
36+
return entrySet.stream()
37+
.findFirst()
38+
.get();
39+
}
40+
41+
private Map<Integer, String> populateMapValues(Map<Integer, String> map) {
42+
if (map != null) {
43+
map.put(5, "A");
44+
map.put(1, "B");
45+
map.put(2, "C");
46+
}
47+
return map;
2248
}
2349

2450
@Test
2551
public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
2652
Map<Integer, String> hashMap = new HashMap<>();
27-
hashMap = mapFirstPairExample.populateMapValues(hashMap);
53+
hashMap = populateMapValues(hashMap);
2854

29-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap);
55+
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(hashMap);
3056
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
3157
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
3258

@@ -37,8 +63,8 @@ public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst()
3763
@Test
3864
public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
3965
Map<Integer, String> hashMap = new HashMap<>();
40-
hashMap = mapFirstPairExample.populateMapValues(hashMap);
41-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap);
66+
hashMap = populateMapValues(hashMap);
67+
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(hashMap);
4268
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
4369
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
4470

@@ -49,8 +75,8 @@ public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
4975
@Test
5076
public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
5177
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
52-
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
53-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap);
78+
linkedHashMap = populateMapValues(linkedHashMap);
79+
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(linkedHashMap);
5480
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
5581

5682
assertEquals(expectedValue, actualValue);
@@ -59,9 +85,9 @@ public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst
5985
@Test
6086
public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
6187
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
62-
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
88+
linkedHashMap = populateMapValues(linkedHashMap);
6389

64-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap);
90+
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(linkedHashMap);
6591
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
6692

6793
assertEquals(expectedValue, actualValue);
@@ -70,10 +96,10 @@ public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst()
7096
@Test
7197
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() {
7298
Map<Integer, String> hashMap = new HashMap<>();
73-
hashMap = mapFirstPairExample.populateMapValues(hashMap);
99+
hashMap = populateMapValues(hashMap);
74100

75101
hashMap.put(0, "D");
76-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap);
102+
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(hashMap);
77103
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
78104

79105
assertEquals(expectedValue, actualValue);
@@ -82,10 +108,10 @@ public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() {
82108
@Test
83109
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() {
84110
Map<Integer, String> hashMap = new HashMap<>();
85-
hashMap = mapFirstPairExample.populateMapValues(hashMap);
111+
hashMap = populateMapValues(hashMap);
86112

87113
hashMap.put(0, "D");
88-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap);
114+
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(hashMap);
89115
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
90116

91117
assertEquals(expectedValue, actualValue);
@@ -94,10 +120,10 @@ public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() {
94120
@Test
95121
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() {
96122
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
97-
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
123+
linkedHashMap = populateMapValues(linkedHashMap);
98124

99125
linkedHashMap.put(0, "D");
100-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap);
126+
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(linkedHashMap);
101127
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
102128

103129
assertEquals(expectedValue, actualValue);
@@ -106,10 +132,10 @@ public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingI
106132
@Test
107133
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() {
108134
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
109-
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
135+
linkedHashMap = populateMapValues(linkedHashMap);
110136

111137
linkedHashMap.put(0, "D");
112-
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap);
138+
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(linkedHashMap);
113139
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
114140

115141
assertEquals(expectedValue, actualValue);

0 commit comments

Comments
 (0)