Java 13 is coming, 12 must learn from the stack leader!
Java 13 is about to be released. New features must be seen first!
The stack leader shared the Wechat public number on the Java technology stack before< Java 11 has been released, String can play like this! > This article, introduced Java 11's String new play method, let everyone refresh.
Java 12 has been released for several months: Java 12 officially released, 8 new features! Today, I'm going to share three sauce operations about String in Java 12, which are absolutely useful.
For more information about Java, dry goods tutorials, and good news, please pay attention to Wechat Public Number: Java Technology Stack, first time push.
Sit down, ready to take off!
1,transform
Transform: String conversion. Let's look at the source code of transform.
public <R> R transform(Function<? super String, ? extends R> f) { return f.apply(this); }
Input a Function interface Function, accept a value, return a value, reference: Functional Interface for New Java 8 Features.
Say less nonsense and do it directly.
private static void testTransform() { System.out.println("======test java 12 transform======"); List<String> list1 = List.of("Java", " Python", " C++ "); List<String> list2 = new ArrayList<>(); list1.forEach(element -> list2.add(element.transform(String::strip) .transform(String::toUpperCase) .transform((e) -> "Hi," + e)) ); list2.forEach(System.out::println); }
Results Output:
======test java 12 transform====== Hi,JAVA Hi,PYTHON Hi,C++
The example is to convert a string three times in a row. The code is simple enough for everyone to understand.
2,indent
Look directly at the examples:
private static void testIndent() { System.out.println("======test java 12 indent======"); String result = "Java\n Python\nC++".indent(3); System.out.println(result); }
Results Output:
======test java 12 indent====== Java Python C++
Line break n is indented by N spaces, zero or negative numbers are not indented.
The following is the core source code of indent:
private String indent(int n, boolean removeBlanks) { Stream<String> stream = removeBlanks ? lines(Integer.MAX_VALUE, Integer.MAX_VALUE) : lines(); if (n > 0) { final String spaces = " ".repeat(n); stream = stream.map(s -> spaces + s); } else if (n == Integer.MIN_VALUE) { stream = stream.map(s -> s.stripLeading()); } else if (n < 0) { stream = stream.map(s -> s.substring(Math.min(-n, s.indexOfNonWhitespace()))); } return stream.collect(Collectors.joining("\n", "", "\n")); }
In fact, it calls the lines() method to create a Stream, and then splices the specified number of spaces forward.
Reference:< Java 11 has been released, String can play like this! > This article introduces lines().
3,describeConstable
private static void testDescribeConstable() { System.out.println("======test java 12 describeConstable======"); String name = "Java technology stack"; Optional<String> optional = name.describeConstable(); System.out.println(optional.get()); }
Results Output:
======test java 12 describeConstable====== Java technology stack
Java 12, String implements the Constable interface:
java.lang.constant.Constable
This interface has a method, the source code is as follows:
public interface Constable { Optional<? extends ConstantDesc> describeConstable(); }
Java 12 String implementation source code:
@Override public Optional<String> describeConstable() { return Optional.of(this); }
Simply, you call the Optional.of method to return an Optional type. You can refer to this article if Optional does not understand: Optional of New Java 8 Features.
Okay, today's sharing is over. Let's collect and forward it. Learn more and understand it. It will certainly be useful in the future.
Historic Java New Features Dry Goods Sharing:
For this dry article on the Java 8-12 series of new features, please search for Wechat Public Number: Java Technology Stack, and reply in the Public Number Background: java.
New features of Java 12 continue to be updated...
This article was originally published in Wechat Public Number: Java Technology Stack (id:javastack), reprinted please keep the original information.