Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
モダンなJavaの書き方
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Yuichi.Sakuraba
March 01, 2021
Technology
5.4k
3
Share
モダンなJavaの書き方
2021.02.25
TECH Street
Javaエンジニア勉強会資料
Yuichi.Sakuraba
March 01, 2021
More Decks by Yuichi.Sakuraba
See All by Yuichi.Sakuraba
Java 25に至る道
skrb
3
290
Lazy Constant - finalフィールドの遅延初期化
skrb
0
3.6k
Language Update: Java
skrb
2
460
Java 30周年記念! Javaの30年をふりかえる
skrb
4
3.8k
JavaにおけるNull非許容性
skrb
2
3.9k
あなたはJVMの気持ちを理解できるか?
skrb
6
32k
で、ValhallaのValue Classってどうなったの?
skrb
2
15k
Javaにおける関数型プログラミンへの取り組み
skrb
7
700
今こそ、ラムダ式を考える - なぜあなたはラムダ式を苦手と感じるのか
skrb
6
27k
Other Decks in Technology
See All in Technology
多角的な視点から見たAGI
terisuke
0
130
Swift Sequence の便利 API 再発見
treastrain
1
270
AIと乗り切った1,500ページ超のヘルプサイト基盤刷新とさらにその先の話
mugi_uno
2
340
Gaussian Splattingの実用化 - 映像制作への展開
gpuunite_official
0
180
Tachikawa.any 運営挨拶
daitasu
0
170
ESP32 IoTを動かしながらメモリ使用量を観測してみた話
zozotech
PRO
0
110
なぜ、私がCommunity Builderに?〜活動期間1か月半でも選出されたワケ〜
yama3133
0
130
Oracle Exadata Database Service on Cloud@Customer X11M (ExaDB-C@C) サービス概要
oracle4engineer
PRO
2
8k
iOS・Androidの文字サイズ設定をWebViewに!モバイルUIのアクセシビリティTips
shincarpediem
2
100
AI対話分析の夢と、汚いデータの現実 Looker / Dataplex / Dataform で実現する品質ファーストな基盤設計
waiwai2111
0
510
小さいVue.jsを30分で作る
hal_spidernight
0
150
続 運用改善、不都合な真実 〜 物理制約のない運用改善はほとんど無価値 / 20260518-ssmjp-kaizen-no-value-without-physical-constraints
opelab
2
160
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Heart Work Chapter 1 - Part 1
lfama
PRO
6
35k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Visualization
eitanlees
150
17k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.2k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
370
Statistics for Hackers
jakevdp
799
230k
The untapped power of vector embeddings
frankvandijk
2
1.7k
A designer walks into a library…
pauljervisheath
211
24k
KATA
mclloyd
PRO
35
15k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Transcript
モダンなJavaの書き方 Java in the Box 櫻庭 祐一
Agenda Javaが変革する背景 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 速攻入門
Hardware Javaが変革する背景 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のトレンド 可読性の重視 関数の活用 Immutable var switch式 ラムダ式 ... Javaの機能
Javaのトレンド 可読性の重視 関数の活用 Immutable var switch式 ラムダ式 ... ラムダ式 Stream
API Flow (Reactive Stream) Javaの機能
Javaのトレンド 可読性の重視 関数の活用 Immutable var switch式 ラムダ式 ... ラムダ式 Stream
API Flow (Reactive Stream) Record Immutable Collection Factory Javaの機能
Javaのトレンド 可読性の重視 関数の活用 Immutable var switch式 ラムダ式 ... ラムダ式 Stream
API Flow (Reactive Stream) Record Immutable Collection Factory Javaの機能 Java 8 Java 11LTS Java 17LTS
では、どう書く? • ラムダ式を自在に使えるようにする • オブジェクトの状態変化をなるべく避ける ◦ できればImmutableにする
では、どう書く? • ラムダ式を自在に使えるようにする • オブジェクトの状態変化をなるべく避ける ◦ できればImmutableにする 可読性を向上させる Multithread Ready
処理の切り分けを容易にする こう書くことで
とりあえず、やってみよう 顧客クラス 顧客リストから製品IDごとの購買数のマップに変換する public class Customer { … // 購買履歴を製品IDのリストとして返す
public List<Integer> getHistory() { … } }
public Map<Integer, Long> convert(List<Customer> customers) { Map<Integer, Long> productCountMap =
new HashMap<>(); for (int i = 0; i < customers.size(); i++) { Customer customer = customers.get(i); List<Integer> 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<Integer, Long> convert(List<Customer> customers) { Map<Integer, Long> productCountMap =
new HashMap<>(); for (int i = 0; i < customers.size(); i++) { Customer customer = customers.get(i); List<Integer> 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<Integer, Long> convert(List<Customer> customers) { Map<Integer, Long> productCountMap =
new HashMap<>(); for (Customer customer: customers) { List<Integer> 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<Integer, Long> convert(List<Customer> customers) { Map<Integer, Long> productCountMap =
customers.stream() .flatMap(customer -> customer.getHistory().stream()) .collect(Collectors.groupingBy(pId -> pId, Collectors.counting())); return productCountMap; } Java 8以降 マップの要素を変更しないので Immutableにすることも可能 2重ループの展開 トリッキーなif文の排除
Conclusion • 可読性向上 • 関数の活用 • 状態変化を避けること • 新しい言語仕様/機能を積極的に試す を意識してコードを記述