Java 12 does not have many useful features for developers, but it is practical.
String enhancement
Java 12 further enhances string operations by adding two methods.
String indent
String indent(int n) indents the string according to parameter n. The specific rules are
- When n > 0, n spaces will be inserted at the beginning of each line of the string, and the whole string will move to the right.
- When n < 0, n spaces will be deleted at the beginning of each line of the string. If the actual number of spaces is less than N, all spaces in this line will be deleted, but there will be no newline.
Let's experiment:
String text = " Hello \n Java12"; System.out.println("Before indenting"); System.out.println(text); System.out.println("Right indent two characters"); String indent2 = text.indent(2); System.out.println(indent2); System.out.println("The left is indented by three characters, but there is only one space"); String indent3 = text.indent(-3); System.out.println(indent3);
The corresponding results are:
String conversion
String adds a transform method to functionalize string operations.
<R> R transform(Function<? super String, ? extends R> f)
The purpose is to strengthen the function operation of string. for instance:
String txt = "hello "; // hello hello String s = txt.transform(str -> str.repeat(2));
Every version of Java is strengthening functional programming.
Content based file matching
Java 12 adds a new static method Files.mismatch(Path,Path) to the Files tool class to find places where the contents of two Files (bytes) are different, and returns the position of the first mismatched byte in the contents of the two Files. If there is no mismatch, it returns - 1L.
// File comparison Path p1 = Files.createTempFile("file1", "txt"); Path p2 = Files.createTempFile("file2", "txt"); Files.writeString(p1, "felord.cn"); Files.writeString(p2, "felord.cn"); // -1L both contents are the same long mismatch = Files.mismatch(p1, p2);
The effect of this method is similar to that of the other method, Files.isSameFile(Path, Path), but there are still differences.
Collectors::teeing
The aggregation operation Collector of Stream stream Stream is further enhanced by adding a team operation to realize some complex aggregation operations. For example, if I want to count the proportion of the average of an array in the sum, I must first calculate the average, then calculate the sum, and then divide it. This requires three steps.
Double average = Stream.of(1, 2, 3, 4, 5).collect(Collectors.averagingDouble(i -> i)); Double total = Stream.of(1, 2, 3, 4, 5).collect(Collectors.summingDouble(i -> i)); Double percentage = average / total;
After using teeing, you can complete it in one step:
Double meanPercentage = Stream.of(1, 2, 3, 4, 5) .collect(Collectors.teeing( Collectors.averagingDouble(i -> i), Collectors.summingDouble(i -> i), (average, total) -> average / total));
New number formatting
Java 12 introduces a new region based compact digital format class CompactNumberFormat, which is used to abbreviate long numbers. Usually programmers like to mark the salary range as 10k-20k, while other industries like 10000-20000.
NumberFormat chnFormat = NumberFormat.getCompactNumberInstance(Locale.CHINA, NumberFormat.Style.SHORT); chnFormat.setMaximumFractionDigits(3); // 82 thousand and 320 String cformat = chnFormat.format(82323); NumberFormat usFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); usFormat.setMaximumFractionDigits(3); // 82.323K String uformat = usFormat.format(82323);
You can also customize CompactNumberFormat to realize personalized number formatting.
other
In addition to the above, Java 12 also has some preview properties and JVM enhancements. Personally, I don't think there are many highlights.