Java 11 Cookbook
上QQ阅读APP看书,第一时间看更新

New API

There are several additions to the standard Java API:

  • Character.toString(int codePoint): Returns a String object representing the character specified by the provided Unicode code point:
var s = Character.toString(50);
System.out.println(s); //prints: 2
  • CharSequence.compare(CharSequence s1, CharSequence s2): Compares two CharSequence instances lexicographically. Returns the difference between the position of the second parameter and the position of the first parameter in the ordered list:
var i = CharSequence.compare("a", "b");
System.out.println(i); //prints: -1

i = CharSequence.compare("b", "a");
System.out.println(i); //prints: 1

i = CharSequence.compare("this", "that");
System.out.println(i); //prints: 8

i = CharSequence.compare("that", "this");
System.out.println(i); //prints: -8
  • The repeat(int count) method of the String class: Returns a String value composed of count times repeated in the String source value:
String s1 = "a";
String s2 = s1.repeat(3); //prints: aaa
System.out.println(s2);

String s3 = "bar".repeat(3);
System.out.println(s3); //prints: barbarbar
  • The isBlank() method of the String class: Returns true if the String value is empty or contains only white spaces, otherwise false. In our example, we have contrasted it with the isEmpty() method, which returns true if, and only if, length() is zero:
String s1 = "a";
System.out.println(s1.isBlank()); //false
System.out.println(s1.isEmpty()); //false

String s2 = "";
System.out.println(s2.isBlank()); //true
System.out.println(s2.isEmpty()); //true

String s3 = " ";
System.out.println(s3.isBlank()); //true
System.out.println(s3.isEmpty()); //false

  • The lines() method of the String class: Returns a Stream object that emits lines extracted from the source String value, separated by line terminators  \n, \r, or \r\n:
String s = "l1 \nl2 \rl3 \r\nl4 ";
s.lines().forEach(System.out::print); //prints: l1 l2 l3 l4
  • Three methods of the String class that remove leading space, trailing space, or both from the source String value:
String s = " a b ";
System.out.println("'" + s.strip() + "'"); // 'a b'
System.out.println("'" + s.stripLeading() + "'"); // 'a b '
System.out.println("'" + s.stripTrailing() + "'");// ' a b'
  • Two Path.of() methods that construct a java.nio.file.Path object:
Path filePath = Path.of("a", "b", "c.txt");
System.out.println(filePath); //prints: a/b/c.txt

try {
filePath = Path.of(new URI("file:/a/b/c.txt"));
System.out.println(filePath); //prints: /a/b/c.txt
} catch (URISyntaxException e) {
e.printStackTrace();
}
  • The asMatchPredicate() method of the java.util.regex.Pattern class, which creates an object of the java.util.function.Predicate functional interface, which then allows us to test a String value for matching the compiled pattern. In the following example, we test whether a String value starts with the a character and ends with the b character:
Pattern pattern = Pattern.compile("^a.*z$");
Predicate<String> predicate = pattern.asMatchPredicate();
System.out.println(predicate.test("abbbbz")); // true
System.out.println(predicate.test("babbbz")); // false
System.out.println(predicate.test("abbbbx")); // false