Explore Java 12: String Indent, Transform, File Mismatch, Teeing Collector
Java 12 introduces several practical enhancements—including String.indent for adjustable indentation, String.transform for functional string manipulation, Files.mismatch for byte-level file comparison, the new Collectors.teeing collector for combined stream reductions, and CompactNumberFormat for locale-aware compact number formatting—each illustrated with code examples.
String Enhancements
Java 12 adds two new methods for string handling.
String Indent
Method String indent(int n) adjusts indentation based on the integer n. If n>0, n spaces are added at the start of each line; if n<0, up to |n| spaces are removed from each line, possibly deleting all leading spaces without adding new lines.
String text = " Hello
Java12";
System.out.println("Before indent");
System.out.println(text);
System.out.println("Indent by 2");
String indent2 = text.indent(2);
System.out.println(indent2);
System.out.println("Indent by -3");
String indent3 = text.indent(-3);
System.out.println(indent3);String Transform
Method String transform(Function<? super String, ? extends R> f) allows functional transformation of a string.
String txt = "hello ";
String s = txt.transform(str -> str.repeat(2));Java’s each version strengthens functional programming.
File Content Mismatch
Files.mismatch(Path, Path)returns the position of the first differing byte between two files, or -1L if they are identical.
// file comparison
Path p1 = Files.createTempFile("file1", "txt");
Path p2 = Files.createTempFile("file2", "txt");
Files.writeString(p1, "felord.cn");
Files.writeString(p2, "felord.cn");
long mismatch = Files.mismatch(p1, p2); // -1L indicates same contentCollectors::teeing
The new teeing collector enables simultaneous collection of multiple results from a stream, such as computing average and sum then dividing them.
Double meanPercentage = Stream.of(1,2,3,4,5)
.collect(Collectors.teeing(
Collectors.averagingDouble(i -> i),
Collectors.summingDouble(i -> i),
(average, total) -> average / total));Compact Number Formatting
CompactNumberFormatprovides locale‑aware compact representation of large numbers.
NumberFormat cnFormat = NumberFormat.getCompactNumberInstance(Locale.CHINA, NumberFormat.Style.SHORT);
cnFormat.setMaximumFractionDigits(3);
String cformat = cnFormat.format(82323); // 8.232万
NumberFormat usFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
usFormat.setMaximumFractionDigits(3);
String uformat = usFormat.format(82323); // 82.323KOther Notes
Java 12 also includes preview features and JVM enhancements, but no major highlights beyond those mentioned.
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.
