• You have one thought, I have one thought, and when we exchange, one person has two thoughts

  • If you can NOT explain it simply, you do NOT understand it well enough

Now the Demo code and technical articles sorted together Github practice selection, this article is also included in this, convenient for everyone to read view, feel good, also please Star🌟

Java12 Collectors. Teeing is a much-appreciated feature for Java12 Collectors. I think there are a few more Java12 features to try out, so this article comes out

If you’re not currently using Version 12 of Java, that’s fine. SDKMAN manages multiple versions of Java in a unified and flexible way, allowing you to quickly explore new features

String API changes

String.indent()

Java12 indent: Java12 indent: Java12 indent: Java12

This method is very simple. It takes an indented value as an int, where n can be either positive or negative. The difference between adding space and removing space is as follows:

String result = "foo\nbar\nbar2".indent(4);
System.out.println(result);
Copy the code

The print looks something like this (test your eyesight now, take a closer look at the indent dots set in the IDE in the screenshot 😜) :

Calling the indent method automatically adds a newline symbol \n, which is explicitly commented in the implementation to normalize line terminators

** Note: ** is seen as a character for a Tab. For example, we changed the above example slightly:

String result = "foo\nbar\n\tbar2".indent(4);
System.out.println(result);
Copy the code

Take a look at the print and notice the difference:

That’s it. Let’s keep going

String.transform()

The transform method takes a parameter of type Function and generates a completely new form of string

List<String> names = List.of( " Alex"."brian");

List<String> transformedNames = new ArrayList<>();

for (String name : names){
	String transformedName = name.transform(String::strip)
															 .transform(StringUtils::toCamelCase);

	transformedNames.add(transformedName);
}
Copy the code

What’s the difference between this and trimming a string or whatever? Because the argument you take is a Function type, the internal processing logic adds more flexibility when the Function type is used as an input parameter

Files.mismatch(Path, Path)

This API comes in handy when we need to compare the contents of two files to see if they are the same. This method compares two files under path and returns a long value representing the first mismatched byte position. If -1 is returned, the two files are equal. Here’s an example:

Path file1 = Paths.get("/Users/fraser/Documents/projects/personal/learning-demo-collection/jdk12-demo/src/file1.txt");
		Path file2 = Paths.get("/Users/fraser/Documents/projects/personal/learning-demo-collection/jdk12-demo/src/file2.txt");


		try {
			long mismatch = Files.mismatch(file1, file2);
			System.out.println(mismatch);
		} catch (IOException e) {
			e.printStackTrace();
		}
Copy the code

The two files are as follows:

//file1.txt hello everyone, I am a soldier of the day, call me brother kung. //file1.txtCopy the code

View the run result:

Take a look at mismatch’s implementation logic. It has a small algorithm in it

Support for Unicode 11

At a time when emojis are playing an important role in social media channels, it’s more important than ever to support the latest Unicode specification. Java 12 maintains synchronization and supports Unicode 11. Unicode 11 added 684 characters for a total of 137,374 characters and 7 new scripts for a total of 146 scripts.

Switch Expressions(Preview)

This change extends the switch statement. Why do you say so?

  • Statement (we used it that way)
  • Expression (Instead of defining a break statement for each case block, we can simply use arrow syntax)
  • Variable assignment (Using the new switch expression, we can directly assign a switch statement to a variable)
boolean isWeekend = switch (day) 
{
    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> false;
 
    case SATURDAY, SUNDAY -> true;
 
    default -> throw new IllegalStateException("Illegal day entry :: " + day);
};
 
System.out.println(isWeekend);
Copy the code

Note: To use this preview feature, remember that we must explicitly indicate the JVM during application startup with the -enable-Preview flag.

Compact Number Formatting

Large numbers rendered by user interfaces or command-line tools are often difficult to represent. It’s much more intuitive to abbreviate numbers. Front-end in order to have better data presentation form, there are corresponding components implemented very early. This feature is now available to backend partners in java12

Compact numeric representations are easier to read and require less space on the screen without losing the original meaning.

Example: 3.6m is much easier to read than 3.6m

Java 12 introduced a called NumberFormat. GetCompactNumberInstance (Locale, NumberFormat. Style) of the static method. To create a compact number representation, consider an example:

		NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);

		String formattedString = formatter.format(25000L);
		System.out.println(formattedString);
Copy the code

To see the results:

In addition, CompactNumberFormat is a subclass of NumberFormat, and we can customize instances of it (formatting styles, etc.)

conclusion

Java nearly two years upgrade is really too fast, to understand some new functions is always right, we start to practice to try it, after meeting similar needs at least can avoid us to repeat the wheel…..

Soul asking

  1. What is the version of Java in your project?
  2. How would you recommend some tool upgrades during the project?

Personal blog: https://dayarch.top Add my wechat friends, enter the group entertainment learning exchange, note “enter the group”

Welcome to continue to pay attention to the public account: “One soldier of The Japanese Arch”

  • Cutting-edge Java technology dry goods sharing
  • Efficient tool summary | back “tool”
  • Interview question analysis and solution
  • Technical data to receive reply | “data”

To read detective novel thinking easy fun learning Java technology stack related knowledge, in line with the simplification of complex problems, abstract problems concrete and graphical principles of gradual decomposition of technical problems, technology continues to update, please continue to pay attention to……