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
Yuichi.Sakuraba
March 01, 2021
Technology
5.3k
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
280
Lazy Constant - finalフィールドの遅延初期化
skrb
0
3.3k
Language Update: Java
skrb
2
450
Java 30周年記念! Javaの30年をふりかえる
skrb
4
3.7k
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
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
16k
JOAI2026講評会資料(近藤佐介)
element138
1
160
Eight Engineering Unit 紹介資料
sansan33
PRO
3
7.2k
新メンバーのために、シニアエンジニアが環境を作る時代
puku0x
0
1.1k
こんなアーキテクチャ図はいやだ / Anti-pattern in AWS Architecture Diagrams
naospon
1
400
昔はシンプルだった_AmazonS3
kawaji_scratch
0
300
CloudSec JP #005 後締め ~ソフトウェアサプライチェーン攻撃から開発者のシークレットを守る~
lhazy
0
220
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
10
78k
Standards et agents IA : un tour d’horizon de MCP, A2A, ADK et plus encore
glaforge
0
100
AWS Agent Registry の基礎・概要を理解する/aws-agent-registry-intro
ren8k
2
340
ぼくがかんがえたさいきょうのあうとぷっと
yama3133
0
170
AI駆動1on1〜AIに自分を育ててもらう〜
yoshiakiyasuda
0
110
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
180
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
98
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
190
Mind Mapping
helmedeiros
PRO
1
150
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
290
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
Site-Speed That Sticks
csswizardry
13
1.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
The Limits of Empathy - UXLibs8
cassininazir
1
300
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
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 • 可読性向上 • 関数の活用 • 状態変化を避けること • 新しい言語仕様/機能を積極的に試す を意識してコードを記述