Java 8 奇淫异巧

字符串

连接字符串

1
2
String.join(":", "foobar", "foo", "bar");
// => foobar:foo:bar

使用正则表达式过滤

1
2
3
4
5
Pattern pattern = Pattern.compile(".*@gmail\\.com");
Stream.of("bob@gmail.com", "alice@hotmail.com")
.filter(pattern.asPredicate())
.count();
// =>

数字

Java的Integer是带符号的。所以范围-2^31 ~ 2^31-1
Java8之前超过这个范围会抛出异常

1
2
3
4
5
6
try {
Integer.parseInt(string, 10);
}
catch (NumberFormatException e) {
System.err.println("could not parse signed int of " + maxUnsignedInt);
}

Java 8 Integer 可以保存无符号的整型,但是必须通过toUnsignedString才能看到正确的指。

1
2
3
4
long maxUnsignedInt = (1l << 32) - 1;//4294967295
String string = String.valueOf(maxUnsignedInt);
int unsignedInt = Integer.parseUnsignedInt(string, 10);//-1
String string2 = Integer.toUnsignedString(unsignedInt, 10);//4294967295

数学运算

Java 8增加了基本运算的Exact()方法。
当运算溢出时,抛出异常。

普通加法

1
2
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MAX_VALUE + 1); // -2147483648 溢出

Java 8

1
2
3
4
5
6
7
try {
Math.addExact(Integer.MAX_VALUE, 1);
}
catch (ArithmeticException e) {
System.err.println(e.getMessage());
// => integer overflow
}

文件

Files.list()

1
2
3
4
5
6
7
8
try (Stream<Path> stream = Files.list(Paths.get(""))) {
String joined = stream
.map(String::valueOf)
.filter(path -> !path.startsWith("."))
.sorted()
.collect(Collectors.joining("; "));
System.out.println("List: " + joined);
}

Files.find()

1
2
3
4
5
6
7
8
9
10
Path start = Paths.get("");
int maxDepth = 5;
try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) ->
String.valueOf(path).endsWith(".js"))) {
String joined = stream
.sorted()
.map(String::valueOf)
.collect(Collectors.joining("; "));
System.out.println("Found: " + joined);
}

Files.walk()

1
2
3
4
5
6
7
8
9
10
Path start = Paths.get("");
int maxDepth = 5;
try (Stream<Path> stream = Files.walk(start, maxDepth)) {
String joined = stream
.map(String::valueOf)
.filter(path -> path.endsWith(".js"))
.sorted()
.collect(Collectors.joining("; "));
System.out.println("walk(): " + joined);
}

Read and write files
读整个文件,效率低

1
2
3
List<String> lines = Files.readAllLines(Paths.get("res/nashorn1.js"));
lines.add("print('foobar');");
Files.write(Paths.get("res/nashorn1-modified.js"), lines);

一行一行读

1
2
3
4
5
6
try (Stream<String> stream = Files.lines(Paths.get("res/nashorn1.js"))) {
stream
.filter(line -> line.contains("print"))
.map(String::trim)
.forEach(System.out::println);
}

使用缓冲

1
2
3
4
Path path = Paths.get("res/nashorn1.js");
try (BufferedReader reader = Files.newBufferedReader(path)) {
System.out.println(reader.readLine());
}

1
2
3
4
Path path = Paths.get("res/output.js");
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
writer.write("print('Hello World');");
}

或者

1
2
3
4
5
6
7
8
Path path = Paths.get("res/nashorn1.js");
try (BufferedReader reader = Files.newBufferedReader(path)) {
long countPrints = reader
.lines()
.filter(line -> line.contains("print"))
.count();
System.out.println(countPrints);
}

© 2017 Hello World All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero