This commit is contained in:
akeshri 2020-09-09 09:34:10 +05:30
parent 64b7dec1b0
commit ece4d65065
4 changed files with 65 additions and 88 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.collections.mapfirstpair;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapFirstPairExample {
public Map.Entry<Integer, String> getFirstPairUsingIterator(Map<Integer, String> map) {
if (map == null || map.size() == 0)
return null;
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet()
.iterator();
if (iterator.hasNext())
return iterator.next();
return null;
}
public Map.Entry<Integer, String> getFirstPairUsingStream(Map<Integer, String> map) {
if (map == null || map.size() == 0)
return null;
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
return entrySet.stream()
.findFirst()
.get();
}
public Map<Integer, String> populateMapValues(Map<Integer, String> map) {
if (map != null) {
map.put(5, "A");
map.put(1, "B");
map.put(2, "C");
}
return map;
}
}

View File

@ -1,17 +0,0 @@
/**
*
*/
package com.baeldung.collections.mapfirstpair;
import java.util.Map;
/**
* @author ASHWINI
*
*/
public interface MapFirstPairService {
<K, V> Map.Entry<K, V> getFirstPairUsingIterator(Map<K, V> map);
<K, V> Map.Entry<K, V> getFirstPairUsingStream(Map<K, V> map);
}

View File

@ -1,40 +0,0 @@
/**
*
*/
package com.baeldung.collections.mapfirstpair;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author ASHWINI
*
*/
public class MapFirstPairServiceImpl implements MapFirstPairService {
public <K, V> Map.Entry<K, V> getFirstPairUsingIterator(Map<K, V> map) {
if (map == null || map.size() == 0)
return null;
Iterator<Map.Entry<K, V>> iterator = map.entrySet()
.iterator();
if (iterator.hasNext())
return iterator.next();
return null;
}
public <K, V> Map.Entry<K, V> getFirstPairUsingStream(Map<K, V> map) {
if (map == null || map.size() == 0)
return null;
Set<Map.Entry<K, V>> entrySet = map.entrySet();
return entrySet.stream()
.findFirst()
.get();
}
}

View File

@ -11,30 +11,22 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
public class MapFirstPairServiceUnitTest {
public class MapFirstPairUnitTest {
private MapFirstPairService mapFirstPairService;
private MapFirstPairExample mapFirstPairExample;
@Before
public void Setup() {
mapFirstPairService = new MapFirstPairServiceImpl();
}
private void populateMapValues(Map<Integer, String> map) {
if (map != null) {
map.put(5, "A");
map.put(1, "B");
map.put(2, "C");
}
mapFirstPairExample = new MapFirstPairExample();
}
@Test
public void whenUsingIteratorForHashMap() {
public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
@ -43,10 +35,10 @@ public class MapFirstPairServiceUnitTest {
}
@Test
public void whenUsingStreamForHashMap() {
public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
@ -55,21 +47,21 @@ public class MapFirstPairServiceUnitTest {
}
@Test
public void whenUsingIteratorForLinkedHashMap() {
public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
}
@Test
public void whenUsingStreamForLinkedHashMap() {
public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
@ -78,10 +70,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
hashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
assertEquals(expectedValue, actualValue);
@ -90,10 +82,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
hashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
assertEquals(expectedValue, actualValue);
@ -102,10 +94,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
linkedHashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
@ -114,10 +106,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
linkedHashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);