Fixed EJB
This commit is contained in:
parent
9a2827f84e
commit
ec041202ae
|
@ -1,57 +0,0 @@
|
||||||
package com.baeldung.map.iteration;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class MapIteration {
|
|
||||||
|
|
||||||
public ArrayList<String> iterateUsingEntrySet(Map<String, Integer> map) {
|
|
||||||
ArrayList<String> mapKeyValueList = new ArrayList<String>();
|
|
||||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
|
||||||
mapKeyValueList.add(entry.getKey() + ":" + entry.getValue());
|
|
||||||
}
|
|
||||||
return mapKeyValueList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<String> iterateUsingLambda(Map<String, Integer> map) {
|
|
||||||
ArrayList<String> mapKeyValueList = new ArrayList<String>();
|
|
||||||
map.forEach((k, v) -> mapKeyValueList.add(k + ":" + v));
|
|
||||||
return mapKeyValueList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<String> iterateUsingIteratorAndEntry(Map<String, Integer> map) {
|
|
||||||
ArrayList<String> mapKeyValueList = new ArrayList<String>();
|
|
||||||
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 ArrayList<String> iterateUsingKeySetAndForeach(Map<String, Integer> map) {
|
|
||||||
ArrayList<String> mapKeyValueList = new ArrayList<String>();
|
|
||||||
for (String key : map.keySet()) {
|
|
||||||
mapKeyValueList.add(key + ":" + map.get(key));
|
|
||||||
}
|
|
||||||
return mapKeyValueList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<String> iterateUsingStreamAPI(Map<String, Integer> map) {
|
|
||||||
ArrayList<String> mapKeyValueList = new ArrayList<String>();
|
|
||||||
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<String>();
|
|
||||||
for (String key : map.keySet()) {
|
|
||||||
mapKeyList.add(key);
|
|
||||||
}
|
|
||||||
return mapKeyList;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,68 +0,0 @@
|
||||||
package com.baeldung.map.iteration;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
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 iterateUsingEntrySet_test() {
|
|
||||||
MapIteration mapIteration = new MapIteration();
|
|
||||||
ArrayList<String> list = mapIteration.iterateUsingEntrySet(testMap);
|
|
||||||
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void iterateUsingLambda_test() {
|
|
||||||
MapIteration mapIteration = new MapIteration();
|
|
||||||
ArrayList<String> list = mapIteration.iterateUsingLambda(testMap);
|
|
||||||
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void iterateUsingIteratorAndEntry_test() {
|
|
||||||
MapIteration mapIteration = new MapIteration();
|
|
||||||
ArrayList<String> list = mapIteration.iterateUsingIteratorAndEntry(testMap);
|
|
||||||
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void iterateUsingKeySetAndForeach_test() {
|
|
||||||
MapIteration mapIteration = new MapIteration();
|
|
||||||
ArrayList<String> list = mapIteration.iterateUsingKeySetAndForeach(testMap);
|
|
||||||
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void iterateUsingStreamAPI_test() {
|
|
||||||
MapIteration mapIteration = new MapIteration();
|
|
||||||
ArrayList<String> list = mapIteration.iterateUsingStreamAPI(testMap);
|
|
||||||
assertTrue((list.contains(testString1)) && (list.contains(testString2)) && (list.contains(testString3)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void iterateKeys_test() {
|
|
||||||
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