The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
从Java 9 到 Java 17 之 Java 12

There are not many features that are useful to developers in Java 12, but it is still useful.

String enhancement

Java 12 further enhances string operations, adding two methods.

String indent

String.indent(int n) will be based on parametersnIndent the string. The specific rules are

  • whenn>0is inserted at the beginning of each line of the stringnSpace, and the entire string moves right.
  • whenn<0is deleted at the beginning of each line of the stringnspaces, if the actual number of spaces is less thann, deletes all spaces on the line, but does not wrap the line.

Let's experiment:

        String text = " Hello \n Java12";
        System.out.println("Before indenting");
        System.out.println(text);
        System.out.println("Indent two characters to the right");
        String indent2 = text.indent(2);
        System.out.println(indent2);
        System.out.println("Three characters are indented on the left side, but there is actually only one space");
        String indent3 = text.indent(-3);
        System.out.println(indent3);

The corresponding result is:

字符串缩进

string conversion

Stringadded atransformMethod to functionalize string operations.

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

The purpose is to strengthen the functional operation of strings. An example:

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

Every version of Java strengthens functional programming.

Content-based file matching

Java 12 inFilesA new static method has been added to the tool classFiles.mismatch(Path,Path), used to find the content of two documents (byte) If there is a difference, return the position of the first mismatch byte in the contents of the two files. If there is no mismatch, return-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 The content is the same
        long mismatch = Files.mismatch(p1, p2);

This method and another methodFiles.isSameFile(Path, Path)The effects are somewhat similar, but there are still differences.

两者的差异

Collectors::teeing

Aggregation operations on Stream streamsCollectorFurther enhanced and increasedteeingOperations to implement some complex aggregation operations. For example, if I want to count the proportion of the average of an array in the total, I have to first calculate the average, then calculate the total, and then divide. 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;

usedteeingYou can then 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 digital formatting

Java 12 introduces new region-based compact digital format classesCompactNumberFormat, used to simplify long numbers. Usually programmers like to label the salary range as10k-20k, while some other industries like10000-20000

        NumberFormat chnFormat = NumberFormat.getCompactNumberInstance(Locale.CHINA,
                NumberFormat.Style.SHORT);
        chnFormat.setMaximumFractionDigits(3);
        //82.32 million
        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 customizeCompactNumberFormatto achieve personalized digital formatting.

other

In addition to the above, Java12 also has some preview properties and JVM enhancements, but there are not many highlights.

read more
Resource download
PriceFree
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://bcbccb.cn/en/3134.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1

Comments0

经典海外游戏源码海外高端娱乐游戏源码全开源 带教程
Classic overseas game source code overseas high-end entertainment game source code fully open source with tutorial
Someone bought it 6 minutes ago Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish