Skip to content

Commit 5b7e761

Browse files
committed
+
1 parent b4a6892 commit 5b7e761

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.vakin.java8;
2+
3+
import java.util.UUID;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
6+
public class ConcurrentHashMapForEach {
7+
8+
public static void main(String[] args) {
9+
ConcurrentHashMap<Integer, UUID> concurrentHashMap = new ConcurrentHashMap<>();
10+
11+
for (int i = 0; i < 100; i++) {
12+
concurrentHashMap.put(i, UUID.randomUUID());
13+
}
14+
15+
int threshold = 1;
16+
17+
concurrentHashMap.forEachValue(threshold, System.out::println);
18+
19+
concurrentHashMap.forEach((id, uuid) -> {
20+
if (id % 10 == 0) {
21+
System.out.println(String.format("%s: %s", id, uuid));
22+
}
23+
});
24+
25+
UUID searchResult = concurrentHashMap.search(threshold, (id, uuid) -> {
26+
if (String.valueOf(uuid).startsWith(String.valueOf(id))) {
27+
return uuid;
28+
}
29+
return null;
30+
});
31+
32+
System.out.println(searchResult);
33+
}
34+
}

src/io/vakin/java8/NioFileTest.java

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.vakin.java8;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
12+
13+
public class NioFileTest {
14+
15+
public static void main(String[] args) throws IOException {
16+
testWalk();
17+
testFind();
18+
testList();
19+
testLines();
20+
testReader();
21+
testWriter();
22+
testReadWriteLines();
23+
testReaderLines();
24+
}
25+
26+
private static void testReaderLines() throws IOException {
27+
Path path = Paths.get("res/readme.txt");
28+
try (BufferedReader reader = Files.newBufferedReader(path)) {
29+
long countPrints = reader
30+
.lines()
31+
.filter(line -> line.contains("print"))
32+
.count();
33+
System.out.println(countPrints);
34+
}
35+
}
36+
37+
private static void testWriter() throws IOException {
38+
Path path = Paths.get("res/output.txt");
39+
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
40+
writer.write("print('Hello World');");
41+
}
42+
}
43+
44+
private static void testReader() throws IOException {
45+
Path path = Paths.get("res/readme.txt");
46+
try (BufferedReader reader = Files.newBufferedReader(path)) {
47+
System.out.println(reader.readLine());
48+
}
49+
}
50+
51+
private static void testWalk() throws IOException {
52+
Path start = Paths.get("");
53+
int maxDepth = 5;
54+
try (Stream<Path> stream = Files.walk(start, maxDepth)) {
55+
String joined = stream
56+
.map(String::valueOf)
57+
.filter(path -> path.endsWith(".txt"))
58+
.collect(Collectors.joining("; "));
59+
System.out.println("walk(): " + joined);
60+
}
61+
}
62+
63+
private static void testFind() throws IOException {
64+
Path start = Paths.get("");
65+
int maxDepth = 5;
66+
try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) ->
67+
String.valueOf(path).endsWith(".txt"))) {
68+
String joined = stream
69+
.sorted()
70+
.map(String::valueOf)
71+
.collect(Collectors.joining("; "));
72+
System.out.println("find(): " + joined);
73+
}
74+
}
75+
76+
private static void testList() throws IOException {
77+
try (Stream<Path> stream = Files.list(Paths.get(""))) {
78+
String joined = stream
79+
.map(String::valueOf)
80+
.filter(path -> !path.startsWith("."))
81+
.sorted()
82+
.collect(Collectors.joining("; "));
83+
System.out.println("list(): " + joined);
84+
}
85+
}
86+
87+
private static void testLines() throws IOException {
88+
try (Stream<String> stream = Files.lines(Paths.get("res/readme.txt"))) {
89+
stream
90+
.filter(line -> line.contains("print"))
91+
.map(String::trim)
92+
.forEach(System.out::println);
93+
}
94+
}
95+
96+
private static void testReadWriteLines() throws IOException {
97+
List<String> lines = Files.readAllLines(Paths.get("res/readme.txt"));
98+
lines.add("print('foobar');");
99+
Files.write(Paths.get("res", "readme2.txt"), lines);
100+
}
101+
}

src/io/vakin/java8/TimeTest.java

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package io.vakin.java8;
2+
3+
import java.time.Clock;
4+
import java.time.DayOfWeek;
5+
import java.time.Instant;
6+
import java.time.LocalDate;
7+
import java.time.LocalDateTime;
8+
import java.time.LocalTime;
9+
import java.time.Month;
10+
import java.time.ZoneId;
11+
import java.time.format.DateTimeFormatter;
12+
import java.time.format.FormatStyle;
13+
import java.time.temporal.ChronoField;
14+
import java.time.temporal.ChronoUnit;
15+
import java.util.Date;
16+
import java.util.Locale;
17+
18+
public class TimeTest {
19+
20+
public static void main(String[] args) {
21+
localDate();
22+
localDateTime();
23+
localTime();
24+
25+
}
26+
27+
private static void localTime() {
28+
Clock clock = Clock.systemDefaultZone();
29+
long t0 = clock.millis();
30+
System.out.println(t0);
31+
32+
Instant instant = clock.instant();
33+
Date legacyDate = Date.from(instant);
34+
35+
36+
ZoneId zone1 = ZoneId.of("Europe/Berlin");
37+
ZoneId zone2 = ZoneId.of("Brazil/East");
38+
39+
System.out.println(zone1.getRules());
40+
System.out.println(zone2.getRules());
41+
42+
// time
43+
LocalTime now1 = LocalTime.now(zone1);
44+
LocalTime now2 = LocalTime.now(zone2);
45+
46+
System.out.println(now1);
47+
System.out.println(now2);
48+
49+
System.out.println(now1.isBefore(now2)); // false
50+
51+
long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
52+
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
53+
System.out.println(hoursBetween);
54+
System.out.println(minutesBetween);
55+
56+
57+
// create time
58+
59+
LocalTime now = LocalTime.now();
60+
System.out.println(now);
61+
62+
LocalTime late = LocalTime.of(23, 59, 59);
63+
System.out.println(late);
64+
65+
DateTimeFormatter germanFormatter =
66+
DateTimeFormatter
67+
.ofLocalizedTime(FormatStyle.SHORT)
68+
.withLocale(Locale.GERMAN);
69+
70+
LocalTime leetTime = LocalTime.parse("13:37", germanFormatter);
71+
System.out.println(leetTime);
72+
}
73+
74+
private static void localDateTime() {
75+
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
76+
77+
DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
78+
System.out.println(dayOfWeek); // WEDNESDAY
79+
80+
Month month = sylvester.getMonth();
81+
System.out.println(month); // DECEMBER
82+
83+
long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
84+
System.out.println(minuteOfDay); // 1439
85+
86+
Instant instant = sylvester
87+
.atZone(ZoneId.systemDefault())
88+
.toInstant();
89+
90+
Date legacyDate = Date.from(instant);
91+
System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014
92+
93+
94+
DateTimeFormatter formatter =
95+
DateTimeFormatter
96+
.ofPattern("MMM dd, yyyy - HH:mm");
97+
98+
LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
99+
String string = parsed.format(formatter);
100+
System.out.println(string);
101+
}
102+
103+
private static void localDate() {
104+
LocalDate today = LocalDate.now();
105+
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
106+
LocalDate yesterday = tomorrow.minusDays(2);
107+
108+
System.out.println(today);
109+
System.out.println(tomorrow);
110+
System.out.println(yesterday);
111+
112+
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
113+
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
114+
System.out.println(dayOfWeek); // FRIDAY
115+
116+
DateTimeFormatter germanFormatter =
117+
DateTimeFormatter
118+
.ofLocalizedDate(FormatStyle.MEDIUM)
119+
.withLocale(Locale.GERMAN);
120+
121+
LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);
122+
System.out.println(xmas); // 2014-12-24
123+
}
124+
125+
}

0 commit comments

Comments
 (0)