BAEL-9040 Two quick improvements to the foreach article

-Added Map's forEach BiConsumer example
This commit is contained in:
Dhawal Kapil 2018-10-23 22:00:06 +05:30
parent dfd549e665
commit 6ef143f570
1 changed files with 14 additions and 4 deletions

View File

@ -1,9 +1,5 @@
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));
}
}