1、record快速定义类
@Test
public void testRecord() {
/**
* JDK16新特性
*
* @param start
* @param end
*/
record qiu(int start, int end) {
int sum() {
return qiu.this.start + qiu.this.end;
}
}
qiu q1 = new qiu(1, 2);
System.out.println("q1 = " + q1);
qiu q2 = new qiu(3, 4);
System.out.println("q2 = " + q2);
System.out.println(q1.equals(q2));
// 调用方法
System.out.println("q1.sum() = " + q1.sum());
System.out.println("q2.sum() = " + q2.sum());
}
2、字符串的新方法
/**
* isBlank():如果字符串为空或字符串仅包含空格(包括制表符),则返回 true。注意与isEmpty() 不同,isEmpty()仅在长度为 0 时返回 true。
* lines():将字符串拆分为字符串流,每个字符串包含一行。
* strip() : 分别从开头和结尾;
* stripLeading()/stripTrailing()仅开始和仅结束删除空格。
* repeat(int times):返回一个字符串,该字符串采用原始字符串并按指定的次数重复该字符串。
* readString():允许从文件路径直接读取到字符串。
* writeString(Path path):将字符串直接写入指定路径处的文件。
* indent(int level):缩进字符串的指定量。负值只会影响前导空格。
* transform(Function f):将给定的 lambda 应用于字符串。
*/
@SneakyThrows
@Test
public void testNewStringMethod() {
String str1 = "";
System.out.println(str1.isBlank()); // true
System.out.println(str1.isEmpty()); // true
str1 = " ";
System.out.println(str1.isBlank()); // true
System.out.println(str1.isEmpty()); // false
str1 = "\r";
System.out.println(str1.isBlank()); // true
System.out.println(str1.isEmpty()); // false
str1 = "\r\n";
System.out.println(str1.isBlank()); // true
System.out.println(str1.isEmpty()); // false
System.out.println("============================================================");
String str2 = "hello qiu \r\n qiu 我的最爱 JDK17 \n\r!\r";
Stream<String> stream = str2.lines();
stream.forEach(System.out::println);
System.out.println("============================================================");
String strip = str2.strip();
System.out.println("strip = " + strip);
System.out.println("str2.stripTrailing() = " + str2.stripTrailing());
System.out.println("str2.stripIndent() = " + str2.stripIndent());
System.out.println("str2.stripLeading() = " + str2.stripLeading());
System.out.println("============================================================");
String str3 = "qiu";
String repeat = str3.repeat(3);
System.out.println("repeat = " + repeat);
System.out.println("============================================================");
String path = "F:\\a.txt";
Path of = Path.of(path);
String string = Files.readString(of);
System.out.println("readString = " + string);
List<String> asList = Arrays.asList("1", "2", "3", "4", "5");
// Files.write(Paths.get("./test"),list, StandardOpenOption.APPEND);
Files.write(Paths.get("F:\\a.txt"), asList, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
Path writeString = Files.writeString(of, "writeString", StandardOpenOption.APPEND);
System.out.println("writeString = " + writeString);
System.out.println("============================================================");
String path3 = "F:\\a.txt";
System.out.println(path3.indent(2));
}
3、switch优雅的写法
// 以前的写法
String result = "";
int caseNum = 4;
switch (caseNum){
case 1 :
result = "zs";
break;
case 2 :
result = "ls";
break;
case 3 :
result = "ww";
break;
default:
result = "qiuqiu";
}
System.out.println("result = " + result);
// 现在的写法
result = switch (caseNum) {
case 1 -> "张三";
case 2 -> "李四";
case 3 -> "王五";
default -> "秋秋";
};
System.out.println("result = " + result);
标签:result,JDK,str1,System,更新,字符串,特性,println,out
From: https://www.cnblogs.com/qbbit/p/16910913.html