Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

Comment below, Nuggets officials are giving away perimeter what was wrong with the welcome comment section of this post stating that nuggets officials are giving away 100 perimeter

Java 12 doesn’t have a lot of features that are useful to developers, but there is some practicality.

String enhancement

Java 12 further enhances string manipulation by adding two methods.

Indent string

String indent(int n) indents the String based on the argument n. The specific rules are

  • whenn>0Is inserted at the beginning of each line of the stringnThe string moves to the right as a whole.
  • whenn<0Is deleted at the beginning of each line of the stringnSpaces, if the actual number of Spaces is less thann, removes all Spaces in the line, but does not wrap.

Here’s an experiment:

         String text = " Hello \n Java12";
         System.out.println("Indent forward");
         System.out.println(text);
         System.out.println("Right indent two characters.");
         String indent2 = text.indent(2);
         System.out.println(indent2);
         System.out.println("Indent three characters to the left, but there's only one space.");
         String indent3 = text.indent(-3);
         System.out.println(indent3);
Copy the code

The corresponding result is:

String conversion

String adds a transform method to functionalize String operations.

 <R> R transform(Function<? super String, ? extends R> f)
Copy the code

The goal is to enforce functional manipulation of strings. Here’s an example:

         String txt = "hello ";
         // hello hello
         String s = txt.transform(str -> str.repeat(2));
Copy the code

Functional programming is enhanced with every release of Java.

Content-based file matching

Java 12 added a new static method files.mismatch (Path,Path) in the Files utility class, which looks for differences between two file contents (byte) and returns the location of the first mismatched byte in the two file contents, or -1L if there is no mismatch.

         // 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 have the same content
         long mismatch = Files.mismatch(p1, p2);
Copy the code

This method is somewhat similar to the other files.issamefile (Path, Path) method, but there are differences.

Collectors::teeing

The Stream aggregation operation Collector is further enhanced with the addition of the Teeing operation for some complex aggregation operations. For example, if I want to count the average of an array in proportion to the sum, I first calculate the average, then calculate the sum, and then divide. This takes 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;
Copy the code

Using teeing can be done 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));
Copy the code

New number formatting

Java 12 introduced a new location-based compact digital format class, CompactNumberFormat, for abbreviating long numbers. Programmers generally prefer a salary range of 10K to 20K, while other industries prefer 10k to 20000.

         NumberFormat chnFormat = NumberFormat.getCompactNumberInstance(Locale.CHINA,
                 NumberFormat.Style.SHORT);
         chnFormat.setMaximumFractionDigits(3);
         / / 82320
         String cformat = chnFormat.format(82323);
  
         NumberFormat usFormat = NumberFormat.getCompactNumberInstance(Locale.US,
                 NumberFormat.Style.SHORT);
         usFormat.setMaximumFractionDigits(3);
         / / 82.323 K
         String uformat = usFormat.format(82323);
Copy the code

You can also customize CompactNumberFormat for personalized number formatting.

other

In addition to the above, Java12 also has some preview properties and JVM enhancements that I don’t think are very exciting.