Skip to content

Commit aa8a782

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents b3d4ed5 + 1fe4967 commit aa8a782

2 files changed

Lines changed: 89 additions & 19 deletions

File tree

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

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.baeldung.java8;
22

3-
import org.junit.Test;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
6-
3+
import java.util.ArrayDeque;
74
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Queue;
11+
import java.util.Set;
912
import java.util.function.Consumer;
1013

14+
import org.junit.Test;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
1118
public class Java8ForEachUnitTest {
1219

1320
private static final Logger LOG = LoggerFactory.getLogger(Java8ForEachUnitTest.class);
@@ -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,55 @@ 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");
96+
97+
namesMap.entrySet()
98+
.forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue()));
99+
}
100+
101+
@Test
102+
public void givenMap_whenUsingBiConsumer_thenIterateAndPrintResults2() {
103+
Map<Integer, String> namesMap = new HashMap<>();
104+
namesMap.put(1, "Larry");
105+
namesMap.put(2, "Steve");
106+
namesMap.put(3, "James");
53107

108+
namesMap.forEach((key, value) -> System.out.println(key + " " + value));
54109
}
55110

56111
}

‎java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
package com.baeldung.zoneddatetime;
22

3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
35
import java.time.LocalDateTime;
46
import java.time.ZoneId;
57
import java.time.ZonedDateTime;
68
import java.time.format.DateTimeFormatter;
9+
import java.time.format.DateTimeParseException;
710
import java.util.logging.Logger;
811

9-
import org.junit.Test;
12+
import org.junit.jupiter.api.Test;
1013

1114
public class ZonedDateTimeUnitTest {
1215

1316
private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName());
1417

1518
@Test
16-
public void testZonedDateTimeToString() {
19+
public void givenZonedDateTime_whenConvertToString_thenOk() {
1720

1821
ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));
1922
ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC"));
2023

2124
LocalDateTime localDateTime = LocalDateTime.now();
2225
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
2326

24-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss Z");
27+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss Z");
2528
String formattedString = zonedDateTime.format(formatter);
2629

27-
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss z");
30+
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss z");
2831
String formattedString2 = zonedDateTime.format(formatter2);
2932

3033
log.info(formattedString);
@@ -33,9 +36,21 @@ public void testZonedDateTimeToString() {
3336
}
3437

3538
@Test
36-
public void testZonedDateTimeFromString() {
39+
public void givenString_whenParseZonedDateTime_thenOk() {
40+
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00");
3741

38-
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00", DateTimeFormatter.ISO_ZONED_DATE_TIME);
42+
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
43+
}
44+
45+
@Test
46+
public void givenString_whenParseZonedDateTimeWithoutZone_thenException() {
47+
assertThrows(DateTimeParseException.class, () -> ZonedDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME));
48+
}
49+
50+
@Test
51+
public void givenString_whenParseLocalDateTimeAtZone_thenOk() {
52+
ZoneId timeZone = ZoneId.systemDefault();
53+
ZonedDateTime zonedDateTime = LocalDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME).atZone(timeZone);
3954

4055
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
4156
}

0 commit comments

Comments
 (0)