|
| 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 = areEqualKeyValues(asiaCapital3, asiaCapital4); |
| 102 | + |
| 103 | + assertEquals(3, result.size()); |
| 104 | + assertThat(result, hasEntry("Japan", false)); |
| 105 | + assertThat(result, hasEntry("South Korea", true)); |
| 106 | + assertThat(result, hasEntry("China", true)); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void givenDifferentMaps_whenGetDiffUsingGuava_thenSuccess() { |
| 111 | + Map<String, String> asia1 = new HashMap<String, String>(); |
| 112 | + asia1.put("Japan", "Tokyo"); |
| 113 | + asia1.put("South Korea", "Seoul"); |
| 114 | + asia1.put("India", "New Delhi"); |
| 115 | + |
| 116 | + Map<String, String> asia2 = new HashMap<String, String>(); |
| 117 | + asia2.put("Japan", "Tokyo"); |
| 118 | + asia2.put("China", "Beijing"); |
| 119 | + asia2.put("India", "Delhi"); |
| 120 | + |
| 121 | + MapDifference<String, String> diff = Maps.difference(asia1, asia2); |
| 122 | + Map<String, ValueDifference<String>> entriesDiffering = diff.entriesDiffering(); |
| 123 | + |
| 124 | + assertFalse(diff.areEqual()); |
| 125 | + assertEquals(1, entriesDiffering.size()); |
| 126 | + assertThat(entriesDiffering, hasKey("India")); |
| 127 | + assertEquals("New Delhi", entriesDiffering.get("India").leftValue()); |
| 128 | + assertEquals("Delhi", entriesDiffering.get("India").rightValue()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void givenDifferentMaps_whenGetEntriesOnOneSideUsingGuava_thenSuccess() { |
| 133 | + Map<String, String> asia1 = new HashMap<String, String>(); |
| 134 | + asia1.put("Japan", "Tokyo"); |
| 135 | + asia1.put("South Korea", "Seoul"); |
| 136 | + asia1.put("India", "New Delhi"); |
| 137 | + |
| 138 | + Map<String, String> asia2 = new HashMap<String, String>(); |
| 139 | + asia2.put("Japan", "Tokyo"); |
| 140 | + asia2.put("China", "Beijing"); |
| 141 | + asia2.put("India", "Delhi"); |
| 142 | + |
| 143 | + MapDifference<String, String> diff = Maps.difference(asia1, asia2); |
| 144 | + Map<String, String> entriesOnlyOnRight = diff.entriesOnlyOnRight(); |
| 145 | + Map<String, String> entriesOnlyOnLeft = diff.entriesOnlyOnLeft(); |
| 146 | + |
| 147 | + assertEquals(1, entriesOnlyOnRight.size()); |
| 148 | + assertThat(entriesOnlyOnRight, hasEntry("China", "Beijing")); |
| 149 | + assertEquals(1, entriesOnlyOnLeft.size()); |
| 150 | + assertThat(entriesOnlyOnLeft, hasEntry("South Korea", "Seoul")); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void givenDifferentMaps_whenGetCommonEntriesUsingGuava_thenSuccess() { |
| 155 | + Map<String, String> asia1 = new HashMap<String, String>(); |
| 156 | + asia1.put("Japan", "Tokyo"); |
| 157 | + asia1.put("South Korea", "Seoul"); |
| 158 | + asia1.put("India", "New Delhi"); |
| 159 | + |
| 160 | + Map<String, String> asia2 = new HashMap<String, String>(); |
| 161 | + asia2.put("Japan", "Tokyo"); |
| 162 | + asia2.put("China", "Beijing"); |
| 163 | + asia2.put("India", "Delhi"); |
| 164 | + |
| 165 | + MapDifference<String, String> diff = Maps.difference(asia1, asia2); |
| 166 | + Map<String, String> entriesInCommon = diff.entriesInCommon(); |
| 167 | + |
| 168 | + assertEquals(1, entriesInCommon.size()); |
| 169 | + assertThat(entriesInCommon, hasEntry("Japan", "Tokyo")); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void givenSimilarMapsWithArrayValue_whenCompareUsingGuava_thenFail() { |
| 174 | + MapDifference<String, String[]> diff = Maps.difference(asiaCity1, asiaCity2); |
| 175 | + assertFalse(diff.areEqual()); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void givenSimilarMapsWithArrayValue_whenCompareUsingGuavaEquivalence_thenSuccess() { |
| 180 | + Equivalence<String[]> eq = new Equivalence<String[]>() { |
| 181 | + @Override |
| 182 | + protected boolean doEquivalent(String[] a, String[] b) { |
| 183 | + return Arrays.equals(a, b); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + protected int doHash(String[] value) { |
| 188 | + return value.hashCode(); |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + MapDifference<String, String[]> diff = Maps.difference(asiaCity1, asiaCity2, eq); |
| 193 | + assertTrue(diff.areEqual()); |
| 194 | + |
| 195 | + diff = Maps.difference(asiaCity1, asiaCity3, eq); |
| 196 | + assertFalse(diff.areEqual()); |
| 197 | + } |
| 198 | + |
| 199 | + // =========================================================================== |
| 200 | + |
| 201 | + private boolean areEqual(Map<String, String> first, Map<String, String> second) { |
| 202 | + if (first.size() != second.size()) { |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + return first.entrySet() |
| 207 | + .stream() |
| 208 | + .allMatch(e -> e.getValue() |
| 209 | + .equals(second.get(e.getKey()))); |
| 210 | + } |
| 211 | + |
| 212 | + private boolean areEqualWithArrayValue(Map<String, String[]> first, Map<String, String[]> second) { |
| 213 | + if (first.size() != second.size()) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + return first.entrySet() |
| 218 | + .stream() |
| 219 | + .allMatch(e -> Arrays.equals(e.getValue(), second.get(e.getKey()))); |
| 220 | + } |
| 221 | + |
| 222 | + private Map<String, Boolean> areEqualKeyValues(Map<String, String> first, Map<String, String> second) { |
| 223 | + return first.entrySet() |
| 224 | + .stream() |
| 225 | + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().equals(second.get(e.getKey())))); |
| 226 | + } |
| 227 | + |
| 228 | +} |
0 commit comments