Skip to content

Commit b2f3115

Browse files
committed
BAEL-9040 Two quick improvements to the foreach article
-Added more tests for forEach tutorial
1 parent 5597bf6 commit b2f3115

1 file changed

Lines changed: 54 additions & 9 deletions

File tree

‎core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java‎

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

7+
import java.util.ArrayDeque;
78
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.HashSet;
812
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Queue;
15+
import java.util.Set;
916
import java.util.function.Consumer;
1017

1118
public class Java8ForEachUnitTest {
@@ -29,8 +36,18 @@ public void compareForEachMethods_thenPrintResults() {
2936
}
3037

3138
// Java 8 - forEach
32-
LOG.debug("--- forEach method ---");
33-
names.forEach(name -> LOG.debug(name));
39+
names.forEach(name -> {
40+
System.out.println(name);
41+
});
42+
43+
LOG.debug("--- Print Consumer ---");
44+
Consumer<String> printConsumer = new Consumer<String>() {
45+
public void accept(String name) {
46+
System.out.println(name);
47+
};
48+
};
49+
50+
names.forEach(printConsumer);
3451

3552
// Anonymous inner class that implements Consumer interface
3653
LOG.debug("--- Anonymous inner class ---");
@@ -40,17 +57,45 @@ public void accept(String name) {
4057
}
4158
});
4259

43-
// Create a Consumer implementation to then use in a forEach method
44-
Consumer<String> consumerNames = name -> {
45-
LOG.debug(name);
46-
};
47-
LOG.debug("--- Implementation of Consumer interface ---");
48-
names.forEach(consumerNames);
60+
// Java 8 - forEach - Lambda Syntax
61+
LOG.debug("--- forEach method ---");
62+
names.forEach(name -> LOG.debug(name));
4963

50-
// Print elements using a Method Reference
64+
// Java 8 - forEach - Print elements using a Method Reference
5165
LOG.debug("--- Method Reference ---");
5266
names.forEach(LOG::debug);
67+
}
68+
69+
@Test
70+
public void givenList_thenIterateAndPrintResults() {
71+
List<String> names = Arrays.asList("Larry", "Steve", "James");
72+
73+
names.forEach(System.out::println);
74+
}
75+
76+
@Test
77+
public void givenSet_thenIterateAndPrintResults() {
78+
Set<String> uniqueNames = new HashSet<>(Arrays.asList("Larry", "Steve", "James"));
79+
80+
uniqueNames.forEach(System.out::println);
81+
}
82+
83+
@Test
84+
public void givenQueue_thenIterateAndPrintResults() {
85+
Queue<String> namesQueue = new ArrayDeque<>(Arrays.asList("Larry", "Steve", "James"));
86+
87+
namesQueue.forEach(System.out::println);
88+
}
89+
90+
@Test
91+
public void givenMap_thenIterateAndPrintResults() {
92+
Map<Integer, String> namesMap = new HashMap<>();
93+
namesMap.put(1, "Larry");
94+
namesMap.put(2, "Steve");
95+
namesMap.put(3, "James");
5396

97+
namesMap.entrySet()
98+
.forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue()));
5499
}
55100

56101
}

0 commit comments

Comments
 (0)