BAEL-725: How to iterate over a map? (#1959)

* BAEL-725: How to iterate over a map?

* Changed the name of the tests in MapIterationTest.java
This commit is contained in:
PRITAM BANERJEE 2017-06-07 13:22:28 -07:00 committed by Zeger Hendrikse
parent 0529906f47
commit 4453358bfb
2 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package com.baeldung.map.iteration;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class MapIteration {
public List<String> iterateUsingEntrySet(Map<String, Integer> map) {
List<String> mapKeyValueList = new ArrayList<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
mapKeyValueList.add(entry.getKey() + ":" + entry.getValue());
}
return mapKeyValueList;
}
public List<String> iterateUsingLambda(Map<String, Integer> map) {
List<String> mapKeyValueList = new ArrayList<>();
map.forEach((k, v) -> mapKeyValueList.add(k + ":" + v));
return mapKeyValueList;
}
public List<String> iterateUsingIteratorAndEntry(Map<String, Integer> map) {
ArrayList<String> mapKeyValueList = new ArrayList<>();
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> pair = iterator.next();
mapKeyValueList.add(pair.getKey() + ":" + pair.getValue());
}
return mapKeyValueList;
}
public List<String> iterateUsingKeySetAndForeach(Map<String, Integer> map) {
List<String> mapKeyValueList = new ArrayList<>();
for (String key : map.keySet()) {
mapKeyValueList.add(key + ":" + map.get(key));
}
return mapKeyValueList;
}
public List<String> iterateUsingStreamAPI(Map<String, Integer> map) {
ArrayList<String> mapKeyValueList = new ArrayList<>();
map.entrySet().stream().forEach(e -> mapKeyValueList.add(e.getKey() + ":" + e.getValue()));
return mapKeyValueList;
}
public ArrayList<String> iterateKeys(Map<String, Integer> map) {
ArrayList<String> mapKeyList = new ArrayList<>();
for (String key : map.keySet()) {
mapKeyList.add(key);
}
return mapKeyList;
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.map.iteration;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Test;
public class MapIterationTest {
public static Map<String, Integer> testMap = new HashMap<String, Integer>();
public static String testString1 = "One:1";
public static String testString2 = "Two:2";
public static String testString3 = "Three:3";
@BeforeClass
public static void createTestData() {
testMap.put("One", 1);
testMap.put("Three", 3);
testMap.put("Two", 2);
}
@Test
public void givenMap_whenIterateUsingEntrySetReturnsAllEntries_thenCorrect() {
MapIteration mapIteration = new MapIteration();
List<String> list = mapIteration.iterateUsingEntrySet(testMap);
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
}
@Test
public void givenMap_whenIterateUsingLambdaReturnsAllEntries_thenCorrect() {
MapIteration mapIteration = new MapIteration();
List<String> list = mapIteration.iterateUsingLambda(testMap);
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
}
@Test
public void givenMap_whenIterateUsingIteratorAndEntryReturnsAllEntries_thenCorrect() {
MapIteration mapIteration = new MapIteration();
List<String> list = mapIteration.iterateUsingIteratorAndEntry(testMap);
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
}
@Test
public void givenMap_whenIterateUsingKeySetAndForeachReturnsAllEntries_thenCorrect() {
MapIteration mapIteration = new MapIteration();
List<String> list = mapIteration.iterateUsingKeySetAndForeach(testMap);
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
}
@Test
public void givenMap_whenIterateUsingStreamAPIReturnsAllEntries_thenCorrect() {
MapIteration mapIteration = new MapIteration();
List<String> list = mapIteration.iterateUsingStreamAPI(testMap);
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
}
@Test
public void givenMap_whenIterateUsingKeysReturnsAllKeys_thenCorrect() {
MapIteration mapIteration = new MapIteration();
ArrayList<String> list = mapIteration.iterateKeys(testMap);
assertTrue((list.contains("One")) && (list.contains("Two")) && (list.contains("Three")));
}
}