Two quick improvements to the foreach article
Added Map's forEach BiConsumer example
package com.baeldung.java8;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
@@ -15,6 +11,10 @@ import java.util.Queue;
import java.util.Set;
import java.util.function.Consumer;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
public class Java8ForEachUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(Java8ForEachUnitTest.class);
@@ -98,4 +98,14 @@ public class Java8ForEachUnitTest {
.forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue()));
}
+ @Test
+ public void givenMap_whenUsingBiConsumer_thenIterateAndPrintResults2() {
+ Map<Integer, String> namesMap = new HashMap<>();
+ namesMap.put(1, "Larry");
+ namesMap.put(2, "Steve");
+ namesMap.put(3, "James");
+
+ namesMap.forEach((key, value) -> System.out.println(key + " " + value));
+ }
+
}
