MapIteration changes (#2072)
This commit is contained in:
parent
2bb31e54ed
commit
b97651ae5a
|
@ -1,58 +1,71 @@
|
|||
package com.baeldung.map.iteration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
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<>();
|
||||
public static void main(String[] args) {
|
||||
MapIteration mapIteration = new MapIteration();
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
|
||||
map.put("One", 1);
|
||||
map.put("Three", 3);
|
||||
map.put("Two", 2);
|
||||
|
||||
System.out.println("Iterating Keys of Map Using KeySet");
|
||||
mapIteration.iterateKeys(map);
|
||||
|
||||
System.out.println("Iterating Map Using Entry Set");
|
||||
mapIteration.iterateUsingEntrySet(map);
|
||||
|
||||
System.out.println("Iterating Using Iterator and Map Entry");
|
||||
mapIteration.iterateUsingIteratorAndEntry(map);
|
||||
|
||||
System.out.println("Iterating Using KeySet and For Each");
|
||||
mapIteration.iterateUsingKeySetAndForeach(map);
|
||||
|
||||
System.out.println("Iterating Map Using Lambda Expression");
|
||||
mapIteration.iterateUsingLambda(map);
|
||||
|
||||
System.out.println("Iterating Using Stream API");
|
||||
mapIteration.iterateUsingStreamAPI(map);
|
||||
}
|
||||
|
||||
public void iterateUsingEntrySet(Map<String, Integer> map) {
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
mapKeyValueList.add(entry.getKey() + ":" + entry.getValue());
|
||||
System.out.println(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 void iterateUsingLambda(Map<String, Integer> map) {
|
||||
map.forEach((k, v) -> System.out.println((k + ":" + v)));
|
||||
}
|
||||
|
||||
public List<String> iterateUsingIteratorAndEntry(Map<String, Integer> map) {
|
||||
ArrayList<String> mapKeyValueList = new ArrayList<>();
|
||||
public void iterateUsingIteratorAndEntry(Map<String, Integer> map) {
|
||||
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());
|
||||
System.out.println(pair.getKey() + ":" + pair.getValue());
|
||||
}
|
||||
|
||||
return mapKeyValueList;
|
||||
}
|
||||
|
||||
public List<String> iterateUsingKeySetAndForeach(Map<String, Integer> map) {
|
||||
List<String> mapKeyValueList = new ArrayList<>();
|
||||
public void iterateUsingKeySetAndForeach(Map<String, Integer> map) {
|
||||
for (String key : map.keySet()) {
|
||||
mapKeyValueList.add(key + ":" + map.get(key));
|
||||
System.out.println(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 void iterateUsingStreamAPI(Map<String, Integer> map) {
|
||||
map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
|
||||
}
|
||||
|
||||
public ArrayList<String> iterateKeys(Map<String, Integer> map) {
|
||||
ArrayList<String> mapKeyList = new ArrayList<>();
|
||||
public void iterateKeys(Map<String, Integer> map) {
|
||||
for (String key : map.keySet()) {
|
||||
mapKeyList.add(key);
|
||||
System.out.println(key);
|
||||
}
|
||||
return mapKeyList;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
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")));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue