Java 12 further enhances string operations by adding two methods.
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.
- If you are learning Spring Boot, we recommend a free tutorial that has been continuously updated for many years: http://blog.didispace.com/spring-boot-learning-2x/
String text = " Hello \n Java12"; System.out.println("Before indenting"); System.out.println(text); System.out.println("Right indent two characters"); //A line break occurred in Java12\n after execution 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);
Before indenting Hello Java12 Right indent two characters Hello Java12 The left is indented by three characters, but there is only one space Hello Java12
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)); hello hello
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); //If the first is not the same, return 0
The effect of this method is similar to that of the other method, Files.isSameFile(Path, Path), but there are still differences.
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.
//The average is 3 Double average = Stream.of(1, 2, 3, 4, 5).collect(Collectors.averagingDouble(i -> i)); //The total is 15 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.
In addition to the above, Java 12 has some preview properties and JVM enhancements, and there are not many highlights.