1. 集合工厂方法:一行代码创建不可变集合
List list = List.of("Java", "Kotlin", "Scala");
Set set = Set.of("Java", "Kotlin", "Scala");
Map map = Map.of( "Java", 1995, "Kotlin", 2011, "Scala", 2004 );
2. InputStream.transferTo():流复制不再繁琐
try (
InputStream is = new FileInputStream("source.txt");
OutputStream os = new FileOutputStream("target.txt")
) {
is.transferTo(os);
}
3. 局部变量类型推断(var):告别冗长的变量声明
var customersByCity = new HashMap>();
var reader = new BufferedReader(new FileReader("data.txt"));
var connection = new URL("https://example.com").openConnection();
for (var entry : customersByCity.entrySet()) {
var city = entry.getKey();
var customers = entry.getValue();
}
4. 不可修改集合的复制方法:集合转换更安全(JDK10)
List original = new ArrayList<>(List.of("Java", "Kotlin", "Scala"));
Set originalSet = new HashSet<>(Set.of(1, 2, 3));
Map originalMap = new HashMap<>(Map.of("one", 1, "two", 2));
List copy = List.copyOf(original);
Set copiedSet = Set.copyOf(originalSet);
Map copiedMap = Map.copyOf(originalMap);
original.add("Groovy");
System.out.println(original);
System.out.println(copy);
try {
copy.add("Clojure");
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify immutable copy");
}
5. String新方法:文本处理得心应手(JDK11)
String multiline = "JavanKotlinnScala";
multiline.lines()
.map(String::toUpperCase)
.forEach(System.out::println);
String text = " Hello World ";
System.out.println(">" + text.strip() + "<");
System.out.println(">" + text.stripLeading() + "<");
System.out.println(">" + text.stripTrailing() + "<");
String unicodeWhitespace = "u2005Hellou2005";
System.out.println(">" + unicodeWhitespace.trim() + "<");
System.out.println(">" + unicodeWhitespace.strip() + "<");
System.out.println(" ".isBlank());
System.out.println("".isBlank());
System.out.println(" a ".isBlank());
String star = "*";
System.out.println(star.repeat(10));
System.out.println("=".repeat(20));
6. Files新方法:文件读写一步到位(JDK11)
String content = Files.readString(Path.of("config.json"));
Files.writeString(Path.of("output.txt"), "Hello Java 11!");
String content = Files.readString(Path.of("data.txt"), StandardCharsets.UTF_8);
Files.writeString(Path.of("log.txt"), "Logged at: " + LocalDateTime.now(),
StandardCharsets.UTF_8);
List lines = List.of("Line 1", "Line 2", "Line 3");
Files.write(Path.of("lines.txt"), lines);
7. Compact Number Formatting:数字的可读性表示(JDK12)
NumberFormat shortFormatter = NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.SHORT);
System.out.println(shortFormatter.format(1000));
System.out.println(shortFormatter.format(1500));
System.out.println(shortFormatter.format(1000000));
System.out.println(shortFormatter.format(1000000000));
NumberFormat longFormatter = NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.LONG);
System.out.println(longFormatter.format(1000));
System.out.println(longFormatter.format(1000000));
NumberFormat germanFormatter = NumberFormat.getCompactNumberInstance(
Locale.GERMANY, NumberFormat.Style.SHORT);
System.out.println(germanFormatter.format(1000));
NumberFormat chineseFormatter = NumberFormat.getCompactNumberInstance(
Locale.CHINA, NumberFormat.Style.SHORT);
System.out.println(chineseFormatter.format(1000));
System.out.println(chineseFormatter.format(1000000));
shortFormatter.setMaximumFractionDigits(1);
System.out.println(shortFormatter.format(1234));
System.out.println(shortFormatter.format(1500));
8. Switch表达式:更简洁的分支处理(JDK14)
String result;
DayOfWeek day = LocalDate.now().getDayOfWeek();
switch (day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
result = "Weekday";
break;
case SATURDAY:
case SUNDAY:
result = "Weekend";
break;
default:
result = "Invalid day";
break;
}
String result = switch (day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday";
case SATURDAY, SUNDAY -> "Weekend";
default -> "Invalid day";
};
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> {
System.out.println("Six letters day");
yield 6;
}
case TUESDAY -> {
System.out.println("Seven letters day");
yield 7;
}
case THURSDAY, SATURDAY -> {
System.out.println("Eight letters day");
yield 8;
}
case WEDNESDAY -> {
System.out.println("Nine letters day");
yield 9;
}
default -> {
throw new IllegalStateException("Invalid day: " + day);
}
};
9. 文本块:多行字符串不再痛苦(JDK15)
String json = "{n" +
" "name": "John Doe",n" +
" "age": 30,n" +
" "address": {n" +
" "street": "123 Main St",n" +
" "city": "Anytown"n" +
" }n" +
"}";
String json = """
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
""";
String html = """
Hello, World!
""";
String query = """
SELECT id, first_name, last_name
FROM employees
WHERE department_id = ?
ORDER BY last_name, first_name
""";
10. instanceof模式匹配:类型检查与转换合二为一(JDK16)
if (obj instanceof String) {
String s = (String) obj;
if (s.length() > 5) {
System.out.println(s.toUpperCase());
}
}
if (obj instanceof String s && s.length() > 5) {
System.out.println(s.toUpperCase());
}
if (obj instanceof String s && s.length() > 10
|| obj instanceof List> list && list.size() > 5) {
}
Object value = getValue();
switch (value) {
case String s when s.length() > 5 -> System.out.println("Long string: " + s);
case String s -> System.out.println("Short string: " + s);
case List> l -> System.out.println("List with " + l.size() + " elements");
default -> System.out.println("Unknown type");
}
11. 增强的伪随机数生成器:更灵活、可预测的随机数(JDK17)
RandomGenerator random = RandomGenerator.getDefault();
System.out.println(random.nextInt(100));
RandomGenerator xoroshiro = RandomGenerator.of("Xoroshiro128PlusPlus");
System.out.println(xoroshiro.nextLong());
RandomGenerator fastRandom = RandomGenerator.of("L32X64MixRandom");
for (int i = 0; i < 5; i++) {
System.out.println(fastRandom.nextInt(1000));
}
RandomGenerator seeded = RandomGenerator.of("Xoshiro256PlusPlus");
((SplittableRandomGenerator) seeded).setSeed(42);
for (int i = 0; i < 5; i++) {
System.out.println(seeded.nextInt(100));
}
DoubleStream randomDoubles = RandomGenerator.getDefault().doubles(1000);
randomDoubles.forEach(System.out::println);
RandomGenerator.all()
.map(provider -> provider.name() + ": " + provider.group())
.sorted()
.forEach(System.out::println);
12. 虚拟线程(Virtual Threads):并发革命(JDK21)
Thread.startVirtualThread(() -> {
System.out.println("Running in virtual thread");
});
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 1000; i++) {
int taskId = i;
executor.submit(() -> {
System.out.println("Task " + taskId + " running on " + Thread.currentThread());
try {
Thread.sleep(Duration.ofMillis(100));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return taskId;
});
}
}
ThreadFactory factory = Thread.ofVirtual().name("worker-", 0).factory();
Thread worker = factory.newThread(() -> {
});
worker.start();
void processFile(Path file) throws IOException {
try (var reader = Files.newBufferedReader(file)) {
String line;
while ((line = reader.readLine()) != null) {
processLine(line);
}
}
}
13. Record模式(Record Patterns):解构数据更简单(JDK21)
record Point(int x, int y) {}
record Rectangle(Point topLeft, Point bottomRight) {}
record Circle(Point center, int radius) {}
Object shape = new Rectangle(new Point(1, 2), new Point(5, 6));
if (shape instanceof Rectangle) {
Rectangle r = (Rectangle) shape;
Point topLeft = r.topLeft();
Point bottomRight = r.bottomRight();
int width = bottomRight.x() - topLeft.x();
int height = bottomRight.y() - topLeft.y();
System.out.println("Rectangle with width " + width + " and height " + height);
}
if (shape instanceof Rectangle(Point(var x1, var y1), Point(var x2, var y2))) {
int width = x2 - x1;
int height = y2 - y1;
System.out.println("Rectangle with width " + width + " and height " + height);
}
String getDescription(Object shape) {
return switch (shape) {
case Rectangle(Point(var x1, var y1), Point(var x2, var y2)) ->
"Rectangle from (%d,%d) to (%d,%d)".formatted(x1, y1, x2, y2);
case Circle(Point(var x, var y), var r) ->
"Circle at (%d,%d) with radius %d".formatted(x, y, r);
default -> "Unknown shape";
};
}
14. 字符串模板(String Templates):安全高效的字符串插值(JDK21预览)
String name = "Alice";
int age = 30;
String message = "Hello, " + name + "! Next year, you'll be " + (age + 1) + ".";
String message = STR."Hello, {name}! Next year, you'll be {age + 1}.";
String status = "active";
String message = STR."User status: {status.toUpperCase()} (set {LocalDate.now()})";
double value = 1234.56789;
String formatted = STR."The value is {value%.2f}";
String json = STR."""
{
"name": "{name}",
"age": {age},
"isAdult": {age >= 18},
"contacts": [
{generateContactsJson()}
]
}
""";
15. 序列集合 (Sequenced Collections):统一的集合操作(JDK21)
SequencedCollection names = new ArrayList<>(List.of("Alice", "Bob", "Charlie"));
String first = names.getFirst();
String last = names.getLast();
names.addFirst("Zoe");
names.addLast("David");
System.out.println(names);
SequencedCollection reversed = names.reversed();
System.out.println(reversed);
SequencedMap scores = new LinkedHashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 85);
scores.put("Charlie", 90);
Map.Entry firstEntry = scores.firstEntry();
Map.Entry lastEntry = scores.lastEntry();
scores.putFirst("Zoe", 100);
scores.putLast("David", 80);
SequencedCollection keys = scores.sequencedKeySet();
SequencedCollection values = scores.sequencedValues();