Unlock Java 11: Powerful New String APIs, Collections, and More
This article walks through Java 11’s most useful enhancements—including new String methods, simplified collection‑to‑array conversion, predicate negation, var in lambda parameters, streamlined file I/O, nested class access rules, and HTTP/2 support—providing concise code examples for each feature.
Java 11 is the latest LTS version after Java 8 and is widely used. This article continues the “Java 9 to Java 17” series by introducing Java 11 features for ordinary developers.
String API enhancements
Java 11 adds several convenient methods to String that reduce the need for extra or complex APIs.
isBlank()
Checks whether a string is empty or contains only whitespace characters after trimming.
String blankStr = " ";
boolean trueVal = blankStr.isBlank(); // truelines()
Splits a string into a Stream of lines using line‑feed ( \n) or carriage‑return ( \r) as delimiters.
String newStr = "Hello Java 11
felord.cn \r 2021-09-28";
Stream<String> lines = newStr.lines();
lines.forEach(System.out::println);
// Hello Java 11
// felord.cn
// 2021-09-28strip()
Removes leading and trailing whitespace, handling both full‑width and half‑width characters.
String str = "HELLO\u3000";
System.out.println("str = " + str.length()); // 6
System.out.println("trim = " + str.trim().length()); // 6
System.out.println("strip = " + str.strip().length()); // 5strip() also has stripLeading() and stripTrailing() variants for removing whitespace only at the start or end.
repeat(n)
Repeats the string a given number of times.
String str = "HELLO";
String empty = str.repeat(0); // ""
String repeatOne = str.repeat(1); // "HELLO"
String repeatTwo = str.repeat(2); // "HELLOHELLO"Convert collections to arrays
Java 11 simplifies converting a collection to an array using the toArray method with a constructor reference.
List<String> sampleList = Arrays.asList("felord.cn", "java 11");
String[] array = sampleList.toArray(String[]::new);Predicate negation
The static method Predicate.not provides a clearer way to negate predicates.
List<String> result = sampleList.stream()
.filter(s -> s.startsWith("j"))
.filter(Predicate.not(s -> s.contains("11")))
.collect(Collectors.toList());Earlier versions also offered a default negate() method.
var in lambda parameters
Java 11 allows var to be used for local variables inside lambda expressions, enabling annotations such as @NotNull.
List<String> result = sampleList.stream()
.filter((@NotNull var s) -> s.startsWith("j"))
.filter(Predicate.not((@NotNull var s) -> s.contains("11")))
.collect(Collectors.toList());File read/write with Strings
The Files class now offers readString and writeString for convenient string I/O.
String dir = "C://yourDir";
Path path = Files.writeString(Files.createTempFile(dir, "hello", ".txt"), "hello java 11");
String fileContent = Files.readString(path);Nested class access rules
Java 11 introduces the NestMembers and NestHost attributes, allowing nested classes to access each other’s private members without synthetic bridge methods.
HttpClient supports HTTP/2
The new java.net.http.HttpClient API supports HTTP/2 and offers asynchronous, non‑blocking operations.
Other improvements
Additional Java 11 enhancements include ZGC, TLS 1.3 support, dynamic invokedynamic, and the open‑sourcing of JFR.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
