Java 8 用法

ref

Java 8 新特性概述
https://www.ibm.com/developerworks/cn/java/j-lo-jdk8newfeature/index.html

Java 8 被动迭代式特性介绍
https://www.ibm.com/developerworks/cn/java/j-lo-java8-iterator/index.html

使用 Java 8 作为 Java 下一代语言
评估 Java 8 是否适合代替 Java
https://www.ibm.com/developerworks/cn/java/j-jn15/index.html

Java 8 中的 Streams API 详解
Streams 的背景,以及 Java 8 中的使用详解
https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/index.html

Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作(aggregate operation),或者大批量数据操作 (bulk data operation)。Stream API 借助于同样新出现的 Lambda 表达式,极大的提高编程效率和程序可读性。同时它提供串行和并行两种模式进行汇聚操作,并发模式能够充分利用多核处理器的优势,使用 fork/join 并行方式来拆分任务和加速处理过程。通常编写并行代码很难而且容易出错, 但使用 Stream API 无需编写一行多线程的代码,就可以很方便地写出高性能的并发程序。所以说,Java 8 中首次出现的 java.util.stream 是一个函数式语言+多核时代综合影响的产物。

The Java 8 Stream API Tutorial
https://www.baeldung.com/java-8-streams

Java 8 Stream Tutorial
https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/

Lambdas and Streams in Java 8 Libraries
http://www.drdobbs.com/jvm/lambdas-and-streams-in-java-8-libraries/240166818

Streams

数组连接成字符串

Joining Objects into a String with Java 8 Stream API

1
2
String[] strs = {"flower","flow","flight"};
System.out.println(Arrays.stream(strs).collect(Collectors.joining( "," )));

返回数组最大值

1
2
int[] arr = {2,7,3,13,59,31};
Arrays.stream(arr).max().getAsInt();

List join as String

1
2
List<Integer> primes = primeNumbersTill(100);
String s = primes.stream().map(Object::toString).collect(Collectors.joining(","));
1
2
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Stream.of(matrix).map(Arrays::toString).forEach(System.out::println);

java 8 打印素数序列

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public static boolean isPrime(int number) {
return IntStream.rangeClosed(2, number/2).noneMatch(i -> number%i == 0);
}

public static List<Integer> primeNumbersTill(int n) {
return IntStream.rangeClosed(2, n)
.filter(x -> isPrime(x)).boxed()
.collect(Collectors.toList());
}

public static void main(String[] args) {
final int n = 100;
List<Integer> primes = primeNumbersTill(n);
String s = primes.stream().map(Object::toString).collect(Collectors.joining(","));
System.out.println(s);
}

2 Sieve of Eratosthenes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public static List<Integer> primeNumber(int n) {
final BitSet sieve = new BitSet(n+1);
List<Integer> plist = IntStream.rangeClosed(2, n)
.filter(x -> !sieve.get(x))
.peek(x -> {
if (x*x < n)
for(int i = x; i <= n; i+=x)
sieve.set(i);
}).boxed()
.collect(Collectors.toList());
return plist;
}

public static void main(String[] args) {
final int n = 100;
List<Integer> plist = primeNumber(n);
System.out.println(plist.stream().map(Object::toString).collect(Collectors.joining(",")));
}

Lambda

LocalDate

1
2
3
LocalDate now = LocalDate.now();
LocalDate firstDayOfYear = now.with(TemporalAdjusters.firstDayOfYear());
System.out.println( firstDayOfYear.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) );