-
《專案實戰(影片)》:從書中學,往事上“練” -
《網際網路高頻面試題》:面朝簡歷學習,春暖花開 -
《架構 x 系統設計》:摧枯拉朽,掌控面試高頻場景題 -
《精進 Java 學習指南》:系統學習,網際網路主流技術棧 -
《必讀 Java 原始碼專欄》:知其然,知其所以然

-
Boot 地址:https://gitee.com/zhijiantianya/ruoyi-vue-pro -
Cloud 地址:https://gitee.com/zhijiantianya/yudao-cloud -
影片教程:https://doc.iocoder.cn


-
虛擬執行緒 -
序列集合 -
記錄模式 -
字串模板(預覽) -
未命名模式和變數(預覽) -
未命名類和例項主要方法(預覽) -
作用域值(預覽) -
結構化併發(預覽)
1 虛擬執行緒

-
提高應用程式吞吐量 -
提高應用程式可用性 -
減少記憶體消耗
建立虛擬執行緒
Thread.ofVirtual()
工廠方法並傳遞可執行物件。-
Thread.ofVirtual().start(Runnable);
-
Thread.ofVirtual().unstarted(Runnable);
start()
方法,它會立即執行傳遞給它的Runnable start()
。unstarted()
方法。建立使用虛擬執行緒的ExecutorService
newFixedThreadPool
為newVirtualThreadPerTaskExecutor
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
publicclassVirtualThreadExample{
publicstaticvoidmain(String[] args){
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
executor.submit(() -> {
System.out.println(Thread.currentThread().getName())
});
executor.shutdown();
}
}
-
專案地址:https://github.com/YunaiV/ruoyi-vue-pro -
影片教程:https://doc.iocoder.cn/video/
2 順序集合

publicinterfaceSequencedCollection<E> extendsCollection<E> {
defaultvoidaddFirst(E e){ ... }
defaultvoidaddLast(E e){ ... }
default E getFirst(){ ... }
default E getLast(){ ... }
default E removeFirst(){ ... }
default E removeLast(){ ... }
SequencedCollection<E> reversed();
}
reverse()
之外的所有方法都是預設方法並提供預設實現。ArrayList<Integer> list = new ArrayList<>();
list.add(1); // [1]
list.addFirst(0); // [0, 1]
list.addLast(2); // [0, 1, 2]
list.getFirst(); // 0
list.getLast(); // 2
list.reversed(); // [2, 1, 0]
SequencedSet
interfaceSequencedSet<E> extendsSet<E>, SequencedCollection<E> {
SequencedSet<E> reversed();
}
SequencedSet<String> values = new LinkedHashSet<>();
values.add("one");
values.add("two");
System.out.println(values); // [one, two]
values.addFirst("zero");
System.out.println(values); // [zero, one, two]
values.addFirst("one");
System.out.println(values); // [one, zero, two]
values.addLast("three");
System.out.println(values); // [one, zero, two, three]
values.removeFirst();
System.out.println(values); // [zero, two, three]
SequencedSet<String> reversedSet = values.reversed();
System.out.println(reversedSet); // [three, two, zero]
boolean isEqual = values.equals(reversedSet);
System.out.println(isEqual); // true
System.out.println(values.hashCode()); // 612888
System.out.println(reversedSet.hashCode()); // 612888
System.out.println(values.hashCode() == reversedSet.hashCode()); // true
SequencedMap
interfaceSequencedMap<K,V> extendsMap<K,V> {
SequencedMap<K,V> reversed();
SequencedSet<K> sequencedKeySet();
SequencedCollection<V> sequencedValues();
SequencedSet<Entry<K,V>> sequencedEntrySet();
V putFirst(K, V);
V putLast(K, V);
Entry<K, V> firstEntry();
Entry<K, V> lastEntry();
Entry<K, V> pollFirstEntry();
Entry<K, V> pollLastEntry();
}
firstEntry()
和lastEntry()
方法訪問第一個和最後一個元素。pollFirstEntry()
方法將刪除並返回第一個鍵值元素,如果對映為空,則返回 null。reverse()
只會比較元素,而不依賴於它們的順序。SequencedMap<String, Integer> myMap = new LinkedHashMap<>();
myMap.put("one", 1);
myMap.put("two", 2);
System.out.println(myMap); // {one=1, two=2}
Entry<String, Integer> firstEntry = myMap.firstEntry();
System.out.println(firstEntry); // one=1
Entry<String, Integer> lastEntry = myMap.lastEntry();
System.out.println(lastEntry); // two=2
myMap.putFirst("zero", 0);
System.out.println(myMap); // {zero=0, one=1, two=2}
myMap.putFirst("one", -1);
System.out.println(myMap); // {one=-1, zero=0, two=2}
Entry<String, Integer> polledFirstEntry = myMap.pollFirstEntry();
System.out.println(polledFirstEntry); // one=-1
System.out.println(myMap); // {zero=0, two=2}
SequencedMap<String, Integer> reversedMap = myMap.reversed();
System.out.println(reversedMap); // {two=2, zero=0}
boolean isEqual = myMap.equals(reversedMap);
System.out.println(isEqual); // true
System.out.println(myMap.hashCode()); // 692224
System.out.println(reversedMap.hashCode()); // 692224
System.out.println(myMap.hashCode() == reversedMap.hashCode()); // true
-
專案地址:https://github.com/YunaiV/yudao-cloud -
影片教程:https://doc.iocoder.cn/video/
3 字串模板
--enable-preview
啟用字串模板。var greeting = new StringBuilder()
.append("Hello, welcome ")
.append(name)
.toString();
var format = "Good morning %s, It's a beautiful day!";
var text = String.format(format, name);
// Java 15+
var text = format.formatter(name);
var format = new MessageFormat("Good morning {0}, It's a beautiful day!");
var greeting = format.format(name);
現在我們有字串模板來拯救
-
模板處理器:Java 提供了兩種用於執行字串插值的模板處理器:STR 和 FMT -
包含包裝表示式的模板,如 {name} -
點 (.) 字元
package com.mina.stringtemplates;
importstatic java.util.FormatProcessor.FMT;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
publicclassStringTemplateExamples{
publicstatic String greeting(String firstName, String lastName){
return STR."Hello! Good morning \{ firstName } \{ lastName }" ;
}
publicstatic String multiplyWithArithmeticExpressions(int a, int b){
return STR."\{ a } times \{ b } = \{ a * b }" ;
}
publicstatic String multiplyWithJavaExpression(int a, int b){
return STR."\{ a } times \{ b } = \{ Math.multiplyExact(a, b) }" ;
}
// multiplication with floating point numbers rounded to two decimal places using the FMT template processor
publicstatic String multiplyFloatingNumbers(double a, double b){
return FMT."%.2f\{ a } times %.2f\{ b } = %.2f\{ a * b }" ;
}
publicstatic String getErrorResponse(int httpStatus, String errorMessage){
return STR."""
{
"httpStatus": \{ httpStatus },
"errorMessage": "\{ errorMessage }"
}""" ;
}
publicstatic String getCurrentDate(){
return STR."Today's date: \{
LocalDate.now().format(
DateTimeFormatter.ofPattern("yyyy-MM-dd")
) }" ;
}
}
4 記錄模式
TransactionTransaction(String type, double amount)
package com.mina.recordpattern;
publicclassRecordPatternExample{
// I'm using "_" for readability here, this won't compile
publicstatic String getTransactionType(Transaction transaction){
returnswitch (transaction) {
casenull -> thrownew IllegalArgumentException("Transaction can not be null.");
caseTransaction(String type, double amount) when type.equals("Deposit") && amount > 0 -> "Deposit";
caseTransaction(String type, _) when type.equals("Withdrawal") -> "Withdrawal";
default -> "Unknown transaction type";
};
}
record Transaction(String type, double amount){
}
}
5 switch 模式匹配
package com.mina.switchpatternmatching;
import com.mina.switchpatternmatching.SwitchPatternMatchingExample.Transaction.Deposit;
import com.mina.switchpatternmatching.SwitchPatternMatchingExample.Transaction.Withdrawal;
publicclassSwitchPatternMatchingExample{
publicstatic String getTransactionType(Transaction transaction){
returnswitch (transaction) {
casenull:
thrownew IllegalArgumentException("Transaction can't be null.");
case Deposit deposit when deposit.getAmount() > 0: // Guarded pattern with when clause
yield "Deposit";
case Withdrawal withdrawal:
yield "Withdrawal";
default:
yield "Unknown transaction type";
};
}
sealed classTransactionpermitsDeposit, Withdrawal{
privatedouble amount;
publicTransaction(double amount){
this.amount = amount;
}
publicdoublegetAmount(){
return amount;
}
finalclassWithdrawalextendsTransaction{
publicWithdrawal(double amount){
super(amount);
}
}
finalclassDepositextendsTransaction{
publicDeposit(double amount){
super(amount);
}
}
}
}





