Skip to content

Commit 03e852e

Browse files
committed
compare hashmap
1 parent fe31164 commit 03e852e

1 file changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package com.baeldung.java.map.compare;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.collection.IsMapContaining.hasEntry;
5+
import static org.hamcrest.collection.IsMapContaining.hasKey;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import java.util.Arrays;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
18+
import com.google.common.base.Equivalence;
19+
import com.google.common.collect.MapDifference;
20+
import com.google.common.collect.MapDifference.ValueDifference;
21+
import com.google.common.collect.Maps;
22+
23+
public class HashMapComparisonUnitTest {
24+
25+
Map<String, String> asiaCapital1;
26+
Map<String, String> asiaCapital2;
27+
Map<String, String> asiaCapital3;
28+
29+
Map<String, String[]> asiaCity1;
30+
Map<String, String[]> asiaCity2;
31+
Map<String, String[]> asiaCity3;
32+
33+
@Before
34+
public void setup(){
35+
asiaCapital1 = new HashMap<String, String>();
36+
asiaCapital1.put("Japan", "Tokyo");
37+
asiaCapital1.put("South Korea", "Seoul");
38+
39+
asiaCapital2 = new HashMap<String, String>();
40+
asiaCapital2.put("South Korea", "Seoul");
41+
asiaCapital2.put("Japan", "Tokyo");
42+
43+
asiaCapital3 = new HashMap<String, String>();
44+
asiaCapital3.put("Japan", "Tokyo");
45+
asiaCapital3.put("China", "Beijing");
46+
47+
asiaCity1 = new HashMap<String, String[]>();
48+
asiaCity1.put("Japan", new String[] { "Tokyo", "Osaka" });
49+
asiaCity1.put("South Korea", new String[] { "Seoul", "Busan" });
50+
51+
asiaCity2 = new HashMap<String, String[]>();
52+
asiaCity2.put("South Korea", new String[] { "Seoul", "Busan" });
53+
asiaCity2.put("Japan", new String[] { "Tokyo", "Osaka" });
54+
55+
asiaCity3 = new HashMap<String, String[]>();
56+
asiaCity3.put("Japan", new String[] { "Tokyo", "Osaka" });
57+
asiaCity3.put("China", new String[] { "Beijing", "Hong Kong" });
58+
}
59+
60+
@Test
61+
public void whenCompareTwoHashMapsUsingEquals_thenSuccess() {
62+
assertTrue(asiaCapital1.equals(asiaCapital2));
63+
assertFalse(asiaCapital1.equals(asiaCapital3));
64+
}
65+
66+
@Test
67+
public void whenCompareTwoHashMapsWithArrayValuesUsingEquals_thenFail() {
68+
assertFalse(asiaCity1.equals(asiaCity2));
69+
}
70+
71+
@Test
72+
public void whenCompareTwoHashMapsUsingStreamAPI_thenSuccess() {
73+
assertTrue(areEqual(asiaCapital1, asiaCapital2));
74+
assertFalse(areEqual(asiaCapital1, asiaCapital3));
75+
}
76+
77+
@Test
78+
public void whenCompareTwoHashMapsWithArrayValuesUsingStreamAPI_thenSuccess() {
79+
assertTrue(areEqualWithArrayValue(asiaCity1, asiaCity2));
80+
assertFalse(areEqualWithArrayValue(asiaCity1, asiaCity3));
81+
}
82+
83+
@Test
84+
public void whenCompareTwoHashMapKeys_thenSuccess() {
85+
assertTrue(asiaCapital1.keySet().equals(asiaCapital2.keySet()));
86+
assertFalse(asiaCapital1.keySet().equals(asiaCapital3.keySet()));
87+
}
88+
89+
@Test
90+
public void whenCompareTwoHashMapKeyValuesUsingStreamAPI_thenSuccess() {
91+
Map<String, String> asiaCapital3 = new HashMap<String, String>();
92+
asiaCapital3.put("Japan", "Tokyo");
93+
asiaCapital3.put("South Korea", "Seoul");
94+
asiaCapital3.put("China", "Beijing");
95+
96+
Map<String, String> asiaCapital4 = new HashMap<String, String>();
97+
asiaCapital4.put("South Korea", "Seoul");
98+
asiaCapital4.put("Japan", "Osaka");
99+
asiaCapital4.put("China", "Beijing");
100+
101+
Map<String, Boolean> result = areEqualKeys(asiaCapital3, asiaCapital4);
102+
assertEquals(3, result.size());
103+
assertThat(result, hasEntry("Japan", false));
104+
assertThat(result, hasEntry("South Korea", true));
105+
assertThat(result, hasEntry("China", true));
106+
}
107+
108+
@Test
109+
public void givenDifferentMaps_whenGetDiffUsingGuava_thenSuccess() {
110+
Map<String, String> asia1 = new HashMap<String, String>();
111+
asia1.put("Japan", "Tokyo");
112+
asia1.put("South Korea", "Seoul");
113+
asia1.put("India", "New Delhi");
114+
115+
Map<String, String> asia2 = new HashMap<String, String>();
116+
asia2.put("Japan", "Tokyo");
117+
asia2.put("China", "Beijing");
118+
asia2.put("India", "Delhi");
119+
120+
MapDifference<String, String> diff = Maps.difference(asia1, asia2);
121+
Map<String, ValueDifference<String>> entriesDiffering = diff.entriesDiffering();
122+
123+
assertFalse(diff.areEqual());
124+
assertEquals(1, entriesDiffering.size());
125+
assertThat(entriesDiffering, hasKey("India"));
126+
assertEquals("New Delhi", entriesDiffering.get("India").leftValue());
127+
assertEquals("Delhi", entriesDiffering.get("India").rightValue());
128+
}
129+
130+
@Test
131+
public void givenDifferentMaps_whenGetEntriesOnOneSideUsingGuava_thenSuccess() {
132+
Map<String, String> asia1 = new HashMap<String, String>();
133+
asia1.put("Japan", "Tokyo");
134+
asia1.put("South Korea", "Seoul");
135+
asia1.put("India", "New Delhi");
136+
137+
Map<String, String> asia2 = new HashMap<String, String>();
138+
asia2.put("Japan", "Tokyo");
139+
asia2.put("China", "Beijing");
140+
asia2.put("India", "Delhi");
141+
142+
MapDifference<String, String> diff = Maps.difference(asia1, asia2);
143+
Map<String, String> entriesOnlyOnRight = diff.entriesOnlyOnRight();
144+
assertEquals(1, entriesOnlyOnRight.size());
145+
assertThat(entriesOnlyOnRight, hasEntry("China", "Beijing"));
146+
147+
Map<String, String> entriesOnlyOnLeft = diff.entriesOnlyOnLeft();
148+
assertEquals(1, entriesOnlyOnLeft.size());
149+
assertThat(entriesOnlyOnLeft, hasEntry("South Korea", "Seoul"));
150+
}
151+
152+
@Test
153+
public void givenDifferentMaps_whenGetCommonEntriesUsingGuava_thenSuccess() {
154+
Map<String, String> asia1 = new HashMap<String, String>();
155+
asia1.put("Japan", "Tokyo");
156+
asia1.put("South Korea", "Seoul");
157+
asia1.put("India", "New Delhi");
158+
159+
Map<String, String> asia2 = new HashMap<String, String>();
160+
asia2.put("Japan", "Tokyo");
161+
asia2.put("China", "Beijing");
162+
asia2.put("India", "Delhi");
163+
164+
MapDifference<String, String> diff = Maps.difference(asia1, asia2);
165+
Map<String, String> entriesInCommon = diff.entriesInCommon();
166+
167+
assertEquals(1, entriesInCommon.size());
168+
assertThat(entriesInCommon, hasEntry("Japan", "Tokyo"));
169+
}
170+
171+
@Test
172+
public void givenSimilarMapsWithArrayValue_whenCompareUsingGuava_thenFail() {
173+
MapDifference<String, String[]> diff = Maps.difference(asiaCity1, asiaCity2);
174+
assertFalse(diff.areEqual());
175+
}
176+
177+
@Test
178+
public void givenSimilarMapsWithArrayValue_whenCompareUsingGuavaEquivalence_thenSuccess() {
179+
Equivalence<String[]> eq = new Equivalence<String[]>() {
180+
@Override
181+
protected boolean doEquivalent(String[] a, String[] b) {
182+
return Arrays.equals(a, b);
183+
}
184+
185+
@Override
186+
protected int doHash(String[] value) {
187+
return value.hashCode();
188+
}
189+
};
190+
191+
MapDifference<String, String[]> diff = Maps.difference(asiaCity1, asiaCity2, eq);
192+
assertTrue(diff.areEqual());
193+
194+
diff = Maps.difference(asiaCity1, asiaCity3, eq);
195+
assertFalse(diff.areEqual());
196+
}
197+
198+
// ===========================================================================
199+
200+
private boolean areEqual(Map<String, String> first, Map<String, String> second) {
201+
if (first.size() != second.size()) {
202+
return false;
203+
}
204+
205+
return first.entrySet()
206+
.stream()
207+
.allMatch(e -> e.getValue()
208+
.equals(second.get(e.getKey())));
209+
}
210+
211+
private boolean areEqualWithArrayValue(Map<String, String[]> first, Map<String, String[]> second) {
212+
if (first.size() != second.size()) {
213+
return false;
214+
}
215+
216+
return first.entrySet()
217+
.stream()
218+
.allMatch(e -> Arrays.equals(e.getValue(), second.get(e.getKey())));
219+
}
220+
221+
private Map<String, Boolean> areEqualKeys(Map<String, String> first, Map<String, String> second) {
222+
return first.entrySet()
223+
.stream()
224+
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().equals(second.get(e.getKey()))));
225+
}
226+
227+
}

0 commit comments

Comments
 (0)