Merge remote-tracking branch 'eugenp/master'

This commit is contained in:
DOHA 2018-10-25 22:38:35 +03:00
commit aa8a7827f7
2 changed files with 89 additions and 19 deletions

View File

@ -1,13 +1,20 @@
package com.baeldung.java8;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Java8ForEachUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(Java8ForEachUnitTest.class);
@ -29,8 +36,18 @@ public class Java8ForEachUnitTest {
}
// Java 8 - forEach
LOG.debug("--- forEach method ---");
names.forEach(name -> LOG.debug(name));
names.forEach(name -> {
System.out.println(name);
});
LOG.debug("--- Print Consumer ---");
Consumer<String> printConsumer = new Consumer<String>() {
public void accept(String name) {
System.out.println(name);
};
};
names.forEach(printConsumer);
// Anonymous inner class that implements Consumer interface
LOG.debug("--- Anonymous inner class ---");
@ -40,17 +57,55 @@ public class Java8ForEachUnitTest {
}
});
// Create a Consumer implementation to then use in a forEach method
Consumer<String> consumerNames = name -> {
LOG.debug(name);
};
LOG.debug("--- Implementation of Consumer interface ---");
names.forEach(consumerNames);
// Java 8 - forEach - Lambda Syntax
LOG.debug("--- forEach method ---");
names.forEach(name -> LOG.debug(name));
// Print elements using a Method Reference
// Java 8 - forEach - Print elements using a Method Reference
LOG.debug("--- Method Reference ---");
names.forEach(LOG::debug);
}
@Test
public void givenList_thenIterateAndPrintResults() {
List<String> names = Arrays.asList("Larry", "Steve", "James");
names.forEach(System.out::println);
}
@Test
public void givenSet_thenIterateAndPrintResults() {
Set<String> uniqueNames = new HashSet<>(Arrays.asList("Larry", "Steve", "James"));
uniqueNames.forEach(System.out::println);
}
@Test
public void givenQueue_thenIterateAndPrintResults() {
Queue<String> namesQueue = new ArrayDeque<>(Arrays.asList("Larry", "Steve", "James"));
namesQueue.forEach(System.out::println);
}
@Test
public void givenMap_thenIterateAndPrintResults() {
Map<Integer, String> namesMap = new HashMap<>();
namesMap.put(1, "Larry");
namesMap.put(2, "Steve");
namesMap.put(3, "James");
namesMap.entrySet()
.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));
}
}

View File

@ -1,19 +1,22 @@
package com.baeldung.zoneddatetime;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.logging.Logger;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class ZonedDateTimeUnitTest {
private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName());
@Test
public void testZonedDateTimeToString() {
public void givenZonedDateTime_whenConvertToString_thenOk() {
ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));
ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC"));
@ -21,10 +24,10 @@ public class ZonedDateTimeUnitTest {
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss Z");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss Z");
String formattedString = zonedDateTime.format(formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss z");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss z");
String formattedString2 = zonedDateTime.format(formatter2);
log.info(formattedString);
@ -33,9 +36,21 @@ public class ZonedDateTimeUnitTest {
}
@Test
public void testZonedDateTimeFromString() {
public void givenString_whenParseZonedDateTime_thenOk() {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00");
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00", DateTimeFormatter.ISO_ZONED_DATE_TIME);
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
@Test
public void givenString_whenParseZonedDateTimeWithoutZone_thenException() {
assertThrows(DateTimeParseException.class, () -> ZonedDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME));
}
@Test
public void givenString_whenParseLocalDateTimeAtZone_thenOk() {
ZoneId timeZone = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = LocalDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME).atZone(timeZone);
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}