Java SE 12 新增特性
作者:Grey
原文地址:Java SE 12 新增特性
源码
镜像仓库: GitCode:java_new_features
String 类中新增的 API
Java SE 12
内置了这样一个方法:String.indent()
。下面的例子显示了如何将一个多行字符串缩进四个空格。
package git.snippets.jdk12;
/**
* String 新增API
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2022/8/19
* @since 12
*/
public class StringNewAPIDemo {
public static void main(String[] args) {
String s = "I am\na multiline\nString.";
System.out.println(s);
System.out.println(s.indent(4));
}
}
输出结果
I am
a multiline
String.
I am
a multiline
String.Code language: MIPS Assembly (mipsasm)
String.transform()
方法将一个任意的函数应用于一个字符串
package git.snippets.jdk12;
import java.math.BigDecimal;
/**
* String 新增API
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2022/8/19
* @since 12
*/
public class StringNewAPIDemo {
public static void main(String[] args) {
// 将一个任意的函数应用于一个字符串
// 效果等同于 String uppercase = "abcde".toUpperCase();
String uppercase = "abcde".transform(String::toUpperCase);
System.out.println(uppercase);
// 效果等同于 Integer i = Integer.valueOf("12345");
Integer i = "12345".transform(Integer::valueOf);
System.out.println(i);
// 效果等同于 BigDecimal big = new BigDecimal("1234567891011121314151617181920");
BigDecimal big = "1234567891011121314151617181920".transform(BigDecimal::new);
System.out.println(big);
}
}
输出:
ABCDE
12345
1234567891011121314151617181920
File 增强 API
你可以使用Files.mismatch()
方法来比较两个文件的内容。
如果两个文件是一样的,该方法返回 -1 。否则,它返回两个文件不同的第一个字节的位置。如果其中一个文件在检测到差异之前结束,则返回该文件的长度。示例代码如下
package git.snippets.jdk12;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* 文件内容对比
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2021/11/29
* @since 12
*/
public class FileMisMatchTest {
public static void main(String[] args) throws IOException {
match();
}
static void match() throws IOException {
Path pathA = Files.createFile(Paths.get("a.txt"));
Path pathB = Files.createFile(Paths.get("b.txt"));
// 写入相同内容
Files.write(pathA, "abc".getBytes(), StandardOpenOption.WRITE);
Files.write(pathB, "abc".getBytes(), StandardOpenOption.WRITE);
long mismatch = Files.mismatch(pathA, pathB);
System.out.println(mismatch);
// 追加不同内容
Files.write(pathA, "1234".getBytes(), StandardOpenOption.APPEND);
Files.write(pathB, "1321".getBytes(), StandardOpenOption.APPEND);
mismatch = Files.mismatch(pathA, pathB);
System.out.println(mismatch);
// 删除创建的文件
pathA.toFile().deleteOnExit();
pathB.toFile().deleteOnExit();
}
}
分流收集器
对于某些要求,你可能想用两个收集器来终止一个流,而不是一个,并将两个收集器的结果结合起来。
比如,获取随机数流中的最大值和最小值
package git.snippets.jdk12;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 分流收集器
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2022/8/19
* @since 12
*/
public class TeeCollectorTest {
public static void main(String[] args) {
// 以下会报错, 只能终止流一次
Stream<Integer> numbers = new Random().ints(100).boxed();
int min = numbers.collect(Collectors.minBy(Integer::compareTo)).orElseThrow();
int max = numbers.collect(Collectors.maxBy(Integer::compareTo)).orElseThrow();
long range = (long) max - min;
}
}
运行后,报错
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:229)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at git.snippets.jdk12.TeeCollectorTest.main(TeeCollectorTest.java:19)
通过如上日志可以看到:我们只能终止一个流一次。
那么我们如何解决这个问题呢?
一种方法是写一个自定义的收集器,将最小值和最大值累积到一个2元素的int
数组中。
package git.snippets.jdk12;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 分流收集器
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2022/8/19
* @since 12
*/
public class TeeCollectorTest {
public static void main(String[] args) {
// 写一个自定义的收集器,将最小值和最大值累积到一个2元素的int数组中。
Stream<Integer> numbers = new Random().ints(100).boxed();
int[] result = numbers.collect(() -> new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE}, (minMax, i) -> {
if (i < minMax[0]) {
minMax[0] = i;
}
if (i > minMax[1]) {
minMax[1] = i;
}
}, (minMax1, minMax2) -> {
if (minMax2[0] < minMax1[0]) {
minMax1[0] = minMax2[0];
}
if (minMax2[1] > minMax1[1]) {
minMax1[1] = minMax2[1];
}
});
long range = (long) result[1] - result[0];
System.out.println(range);
}
}
这种方法相当复杂,我们可以使用Java SE 12
中引入的 "Teeing Collector "来做。我们可以指定两个收集器(称为下游收集器)和一个合并函数,将两个收集器的结果合并。代码如下
package git.snippets.jdk12;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 分流收集器
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2022/8/19
* @since 12
*/
public class TeeCollectorTest {
public static void main(String[] args) {
// 方法2,使用Tee Collector
Stream<Integer> numbers = new Random().ints(100).boxed();
long range = numbers.collect(Collectors.teeing(Collectors.minBy(Integer::compareTo), Collectors.maxBy(Integer::compareTo), (min, max) -> (long) max.orElseThrow() - min.orElseThrow()));
System.out.println(range);
}
}
新版 Switch 使用方式
我们现在可以通过用逗号分隔多个case
来简化switch
语句,并使用箭头符号来消除容易出错的中断语句,这个特性在Java SE 12
中是预览特性,在Java SE 14
正式引入。代码如下:
注:如果你用Java SE 12
运行上述代码,需要指定--enable-preview
参数,如果使用 Intellij IDEA ,参考How to Enable Java Preview Features and Run Code from IntelliJ IDEA
package git.snippets.jdk12;
package git.snippets.jdk12;
/**
* switch新用法
* 预览特性,在jdk14正式引入
* IDEA 启用 预览功能
* <p>
* <a href="https://foojay.io/today/how-to-run-project-loom-from-intellij-idea/">how to run project loom from intellij idea</a>
* <p>
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2021/11/29
* @since 12
*/
public class NewSwitch {
public static void main(String[] args) {
test("apple");
test2("march");
}
// 在jdk12是预览特性,在14正式引入
static void test(String c) {
switch (c) {
case "apple", "Apple" -> System.out.println("苹果");
case "banana", "Banana" -> System.out.println("香蕉");
}
}
static void test2(String day) {
String season = switch (day) {
case "march", "april", "may" -> "春天";
case "june", "july", "august" -> "夏天";
case "september", "october", "november" -> "秋天";
case "december", "january", "february" -> "冬天";
default -> {
throw new RuntimeException("day error");
}
};
System.out.println("当前季节是:" + season);
}
}
简化数字格式显示
使用静态方法NumberFormat.getCompactNumberInstance()
,我们可以创建一个格式化器。这是一种便于人类阅读的格式,如 "2M "或 "30亿"。
代码见:
package git.snippets.jdk12;
import java.text.NumberFormat;
import java.util.Locale;
/**
* 简化的数字格式可以直接转换数字显示格式,比如 1000 -> 1K,1000000 -> 1M 。
*
* @author <a href="mailto:[email protected]">Grey</a>
* @date 2021/11/29
* @since 12
*/
public class CompactNum {
public static void main(String[] args) {
test();
test2();
test3();
}
static void test() {
System.out.println("Compact Formatting is:");
NumberFormat upvotes = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
System.out.println(upvotes.format(100));
System.out.println(upvotes.format(1000));
System.out.println(upvotes.format(10000));
System.out.println(upvotes.format(100000));
System.out.println(upvotes.format(1000000));
// 设置小数位数
upvotes.setMaximumFractionDigits(1);
System.out.println(upvotes.format(1234));
System.out.println(upvotes.format(123456));
System.out.println(upvotes.format(12345678));
System.out.println(upvotes.format(123456789));
}
static void test2() {
System.out.println("Compact Formatting is:");
NumberFormat upvotes = NumberFormat.getCompactNumberInstance(Locale.CHINA, NumberFormat.Style.SHORT);
System.out.println(upvotes.format(100));
System.out.println(upvotes.format(1000));
System.out.println(upvotes.format(10000));
System.out.println(upvotes.format(100000));
System.out.println(upvotes.format(1000000));
// 设置小数位数
upvotes.setMaximumFractionDigits(1);
System.out.println(upvotes.format(1234));
System.out.println(upvotes.format(123456));
System.out.println(upvotes.format(12345678));
System.out.println(upvotes.format(123456789));
}
static void test3() {
System.out.println("Compact Formatting is:");
NumberFormat nfShort =
NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
NumberFormat nfLong =
NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
System.out.println(" 1,000 short -> " + nfShort.format(1_000));
System.out.println(" 456,789 short -> " + nfShort.format(456_789));
System.out.println(" 2,000,000 short -> " + nfShort.format(2_000_000));
System.out.println("3,456,789,000 short -> " + nfShort.format(3_456_789_000L));
System.out.println();
System.out.println(" 1,000 long -> " + nfLong.format(1_000));
System.out.println(" 456,789 long -> " + nfLong.format(456_789));
System.out.println(" 2,000,000 long -> " + nfLong.format(2_000_000));
System.out.println("3,456,789,000 long -> " + nfLong.format(3_456_789_000L));
}
}
输出结果为:
Compact Formatting is:
100
1K
10K
100K
1M
1.2K
123.5K
12.3M
123.5M
Compact Formatting is:
100
1,000
1万
10万
100万
1,234
12.3万
1234.6万
1.2亿
Compact Formatting is:
1,000 short -> 1K
456,789 short -> 457K
2,000,000 short -> 2M
3,456,789,000 short -> 3B
1,000 long -> 1 thousand
456,789 long -> 457 thousand
2,000,000 long -> 2 million
3,456,789,000 long -> 3 billion
更多
参考资料
Java 12 Features (with Examples)
official Java 12 Release Notes
标签:12,Java,format,System,000,println,java,SE,out From: https://www.cnblogs.com/greyzeng/p/16603349.html