From 2f918653634e0c3a10b425118496cc54d8a7c05b Mon Sep 17 00:00:00 2001 From: akeshri Date: Thu, 10 Sep 2020 09:26:24 +0530 Subject: [PATCH] changes --- .../mapfirstpair/MapFirstPairExample.java | 42 ------------ .../mapfirstpair/MapFirstPairUnitTest.java | 68 +++++++++++++------ 2 files changed, 47 insertions(+), 63 deletions(-) delete mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java deleted file mode 100644 index 8e5fc296d4..0000000000 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.collections.mapfirstpair; - -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -public class MapFirstPairExample { - - public Map.Entry getFirstPairUsingIterator(Map map) { - if (map == null || map.size() == 0) - return null; - - Iterator> iterator = map.entrySet() - .iterator(); - - if (iterator.hasNext()) - return iterator.next(); - - return null; - } - - public Map.Entry getFirstPairUsingStream(Map map) { - if (map == null || map.size() == 0) - return null; - - Set> entrySet = map.entrySet(); - - return entrySet.stream() - .findFirst() - .get(); - } - - public Map populateMapValues(Map map) { - if (map != null) { - map.put(5, "A"); - map.put(1, "B"); - map.put(2, "C"); - } - return map; - } - -} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java index c8198bcc02..b25e0932d8 100644 --- a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java @@ -5,28 +5,54 @@ import static org.junit.Assert.assertNotEquals; import java.util.AbstractMap; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; -import org.junit.Before; import org.junit.Test; public class MapFirstPairUnitTest { - private MapFirstPairExample mapFirstPairExample; + private Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) + return null; - @Before - public void Setup() { + Iterator> iterator = map.entrySet() + .iterator(); - mapFirstPairExample = new MapFirstPairExample(); + if (iterator.hasNext()) + return iterator.next(); + + return null; + } + + private Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) + return null; + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + + private Map populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + return map; } @Test public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -37,8 +63,8 @@ public class MapFirstPairUnitTest { @Test public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); + hashMap = populateMapValues(hashMap); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -49,8 +75,8 @@ public class MapFirstPairUnitTest { @Test public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -59,9 +85,9 @@ public class MapFirstPairUnitTest { @Test public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -70,10 +96,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -82,10 +108,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -94,10 +120,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -106,10 +132,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue);