2021.02.25 TECH Street Javaエンジニア勉強会資料
モダンなJavaの書き方Java in the Box 櫻庭 祐一
View Slide
AgendaJavaが変革する背景Javaのトレンドでは、どう書く?
自己紹介● Java歴 26年○ Java 1.0αから○ 2005年 Java Champion○ 2007年 JJUG創設メンバー● Client Side Java○ Java SE, JavaFX● 各種著作○ Web+DB Press 連載 “見なおそう! モダンJavaの流儀”○ 書籍 Java SE 7/8 速攻入門
HardwareJavaが変革する背景Service/Application
HardwareマルチコアVector Computing/GPGPU大量メモリ超高速通信Javaが変革する背景Service/Application
HardwareマルチコアVector Computing/GPGPU大量メモリ超高速通信Javaが変革する背景Service/Application巨大かつ複雑なシステム頻繁な更新
HardwareマルチコアVector Computing/GPGPU大量メモリ超高速通信Javaが変革する背景Service/Application巨大かつ複雑なシステム頻繁な更新マイクロサービスコンテナAI/ML
Javaのトレンド可読性の重視関数の活用Immutable
Javaのトレンド可読性の重視関数の活用Immutablevarswitch式ラムダ式 ...Javaの機能
Javaのトレンド可読性の重視関数の活用Immutablevarswitch式ラムダ式 ...ラムダ式Stream APIFlow (Reactive Stream)Javaの機能
Javaのトレンド可読性の重視関数の活用Immutablevarswitch式ラムダ式 ...ラムダ式Stream APIFlow (Reactive Stream)RecordImmutable Collection FactoryJavaの機能
Javaのトレンド可読性の重視関数の活用Immutablevarswitch式ラムダ式 ...ラムダ式Stream APIFlow (Reactive Stream)RecordImmutable Collection FactoryJavaの機能Java 8Java 11LTSJava 17LTS
では、どう書く?● ラムダ式を自在に使えるようにする● オブジェクトの状態変化をなるべく避ける○ できればImmutableにする
では、どう書く?● ラムダ式を自在に使えるようにする● オブジェクトの状態変化をなるべく避ける○ できればImmutableにする可読性を向上させるMultithread Ready処理の切り分けを容易にするこう書くことで
とりあえず、やってみよう顧客クラス顧客リストから製品IDごとの購買数のマップに変換するpublic class Customer {…// 購買履歴を製品IDのリストとして返すpublic List getHistory() { … }}
public Map convert(List customers) {Map productCountMap = new HashMap<>();for (int i = 0; i < customers.size(); i++) {Customer customer = customers.get(i);List ids = customer.getHistory();for (int j = 0; j < ids.size(); j++) {int pId = ids.get(j);Long productCount = productCountMap.get(pId);if (productCount == null) {productCountMap.put(pId, 1L);} else {productCount++;productCountMap.put(pId, productCount);}}}return productCountMap;}いにしえの書き方
public Map convert(List customers) {Map productCountMap = new HashMap<>();for (int i = 0; i < customers.size(); i++) {Customer customer = customers.get(i);List ids = customer.getHistory();for (int j = 0; j < ids.size(); j++) {int pId = ids.get(j);Long productCount = productCountMap.get(pId);if (productCount == null) {productCountMap.put(pId, 1L);} else {productCount++;productCountMap.put(pId, productCount);}}}return productCountMap;}いにしえの書き方ミュータブルなマップループ制御とロジックが混在分かりにくいループカウンターNullPointerExceptionが発生するためオートボクシングができない
public Map convert(List customers) {Map productCountMap = new HashMap<>();for (Customer customer: customers) {List ids = customer.getHistory();for (int pId: ids) {Long productCount = productCountMap.get(pId);if (productCount == null) {productCountMap.put(pId, 1L);} else {productCount++;productCountMap.put(pId, productCount);}}}return productCountMap;}Java 5だったらfor-each文に変更
public Map convert(List customers) {Map productCountMap= customers.stream().flatMap(customer -> customer.getHistory().stream()).collect(Collectors.groupingBy(pId -> pId, Collectors.counting()));return productCountMap;}Java 8以降マップの要素を変更しないのでImmutableにすることも可能2重ループの展開トリッキーなif文の排除
Conclusion● 可読性向上● 関数の活用● 状態変化を避けること● 新しい言語仕様/機能を積極的に試すを意識してコードを記述