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.

Programmer DD
Programmer DD
Programmer DD
Explore Java 12: String Indent, Transform, File Mismatch, Teeing Collector

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 content

Collectors::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

CompactNumberFormat

provides 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.323K

Other Notes

Java 12 also includes preview features and JVM enhancements, but no major highlights beyond those mentioned.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaFile I/OStream APINumber FormattingString APIJava12
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.