Skip to content

Commit ef3dbf3

Browse files
committed
🍱 add java8 growing code
1 parent 352a684 commit ef3dbf3

File tree

11 files changed

+296
-0
lines changed

11 files changed

+296
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Base64;
5+
6+
/**
7+
* Base64 增强
8+
*
9+
* @author biezhi
10+
* @date 2018/2/8
11+
*/
12+
public class Base64Demo {
13+
14+
public static void main(String[] args) {
15+
final String text = "Lets Learn Java 8!";
16+
17+
final String encoded = Base64
18+
.getEncoder()
19+
.encodeToString(text.getBytes(StandardCharsets.UTF_8));
20+
System.out.println(encoded);
21+
22+
final String decoded = new String(
23+
Base64.getDecoder().decode(encoded),
24+
StandardCharsets.UTF_8);
25+
System.out.println(decoded);
26+
}
27+
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import java.time.*;
4+
5+
/**
6+
* 新的日期时间 API
7+
*
8+
* @author biezhi
9+
* @date 2018/2/8
10+
*/
11+
public class DateTimeAPI {
12+
13+
public static void main(String[] args) {
14+
// Get the system clock as UTC offset
15+
final Clock clock = Clock.systemUTC();
16+
System.out.println(clock.instant());
17+
System.out.println(clock.millis());
18+
19+
// Get the local date and local time
20+
final LocalDate date = LocalDate.now();
21+
final LocalDate dateFromClock = LocalDate.now(clock);
22+
23+
System.out.println(date);
24+
System.out.println(dateFromClock);
25+
26+
// Get the local date and local time
27+
final LocalTime time = LocalTime.now();
28+
final LocalTime timeFromClock = LocalTime.now(clock);
29+
30+
System.out.println(time);
31+
System.out.println(timeFromClock);
32+
33+
// Get the local date/time
34+
final LocalDateTime datetime = LocalDateTime.now();
35+
final LocalDateTime datetimeFromClock = LocalDateTime.now(clock);
36+
37+
System.out.println(datetime);
38+
System.out.println(datetimeFromClock);
39+
40+
// Get the zoned date/time
41+
final ZonedDateTime zonedDatetime = ZonedDateTime.now();
42+
final ZonedDateTime zonedDatetimeFromClock = ZonedDateTime.now(clock);
43+
final ZonedDateTime zonedDatetimeFromZone = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
44+
45+
System.out.println(zonedDatetime);
46+
System.out.println(zonedDatetimeFromClock);
47+
System.out.println(zonedDatetimeFromZone);
48+
49+
// Get duration between two dates
50+
final LocalDateTime from = LocalDateTime.of(2014, Month.APRIL, 16, 0, 0, 0);
51+
final LocalDateTime to = LocalDateTime.of(2015, Month.APRIL, 16, 23, 59, 59);
52+
53+
final Duration duration = Duration.between(from, to);
54+
System.out.println("Duration in days: " + duration.toDays());
55+
System.out.println("Duration in hours: " + duration.toHours());
56+
57+
58+
}
59+
60+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import java.util.function.Supplier;
4+
5+
public interface DefaulableFactory {
6+
// Interfaces now allow static methods
7+
static Integer create(Supplier<Integer> supplier) {
8+
return supplier.get();
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
/**
4+
* 函数式接口
5+
*
6+
* @author biezhi
7+
* @date 2018/2/8
8+
*/
9+
@FunctionalInterface
10+
public interface Functional {
11+
12+
void method();
13+
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
/**
4+
* 默认方法
5+
*
6+
* @author biezhi
7+
* @date 2018/2/8
8+
*/
9+
@FunctionalInterface
10+
public interface FunctionalDefaultMethods {
11+
12+
void method();
13+
14+
default void defaultMethod() {
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Lambda 表达式
7+
*
8+
* @author biezhi
9+
* @date 2018/2/8
10+
*/
11+
public class Lambda {
12+
13+
public static void main(String[] args) {
14+
Arrays.asList("a", "b", "d").forEach(System.out::println);
15+
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import javax.script.ScriptEngine;
4+
import javax.script.ScriptEngineManager;
5+
import javax.script.ScriptException;
6+
7+
/**
8+
* Nashorn JavaScript引擎
9+
*
10+
* @author biezhi
11+
* @date 2018/2/8
12+
*/
13+
public class NashornDemo {
14+
15+
public static void main(String[] args) throws ScriptException {
16+
ScriptEngineManager manager = new ScriptEngineManager();
17+
ScriptEngine engine = manager.getEngineByName("JavaScript");
18+
19+
System.out.println(engine.getClass().getName());
20+
System.out.println("Result:" + engine.eval("function f() { return 1; }; f() + 1;"));
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import java.util.Optional;
4+
5+
/**
6+
* Optional
7+
*
8+
* @author biezhi
9+
* @date 2018/2/8
10+
*/
11+
public class OptionalDemo {
12+
13+
public static void main(String[] args) {
14+
Optional<String> fullName = Optional.ofNullable(null);
15+
System.out.println("Full Name is set? " + fullName.isPresent());
16+
System.out.println("Full Name: " + fullName.orElse("[none]"));
17+
System.out.println(fullName.map(s -> "Hey " + s + "!").orElse("Hey Stranger!"));
18+
19+
Optional<String> firstName = Optional.of("Tom");
20+
System.out.println("First Name is set? " + firstName.isPresent());
21+
System.out.println("First Name: " + firstName.orElseGet(() -> "[none]"));
22+
System.out.println(firstName.map(s -> "Hey " + s + "!").orElse("Hey Stranger!"));
23+
System.out.println();
24+
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import java.util.Arrays;
4+
import java.util.concurrent.ThreadLocalRandom;
5+
6+
/**
7+
* 并行数组
8+
*
9+
* @author biezhi
10+
* @date 2018/2/8
11+
*/
12+
public class ParallelArrays {
13+
14+
public static void main(String[] args) {
15+
long[] arrayOfLong = new long[20000];
16+
17+
Arrays.parallelSetAll(arrayOfLong,
18+
index -> ThreadLocalRandom.current().nextInt(1000000));
19+
Arrays.stream(arrayOfLong).limit(10).forEach(
20+
i -> System.out.print(i + " "));
21+
System.out.println();
22+
23+
Arrays.parallelSort(arrayOfLong);
24+
Arrays.stream(arrayOfLong).limit(10).forEach(
25+
i -> System.out.print(i + " "));
26+
System.out.println();
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.github.biezhi.java8.growing.jdk8;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
public class Streams {
7+
8+
private enum Status {
9+
OPEN, CLOSED
10+
}
11+
12+
private static final class Task {
13+
private final Status status;
14+
private final Integer points;
15+
16+
Task(final Status status, final Integer points) {
17+
this.status = status;
18+
this.points = points;
19+
}
20+
21+
public Integer getPoints() {
22+
return points;
23+
}
24+
25+
public Status getStatus() {
26+
return status;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return String.format("[%s, %d]", status, points);
32+
}
33+
}
34+
35+
public static void main(String[] args) {
36+
final Collection<Task> tasks = Arrays.asList(
37+
new Task(Status.OPEN, 5),
38+
new Task(Status.OPEN, 13),
39+
new Task(Status.CLOSED, 8)
40+
);
41+
42+
// Calculate total points of all active tasks using sum()
43+
final long totalPointsOfOpenTasks = tasks
44+
.stream()
45+
.filter(task -> task.getStatus() == Status.OPEN)
46+
.mapToInt(Task::getPoints)
47+
.sum();
48+
49+
System.out.println("Total points: " + totalPointsOfOpenTasks);
50+
}
51+
}

0 commit comments

Comments
 (0)