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
3
3.5k
モダンなJavaの書き方
2021.02.25
TECH Street
Javaエンジニア勉強会資料
Yuichi.Sakuraba
March 01, 2021
Tweet
Share
More Decks by Yuichi.Sakuraba
See All by Yuichi.Sakuraba
今こそ、ラムダ式を考える - なぜあなたはラムダ式を苦手と感じるのか
skrb
6
18k
今こそ、ラムダ式を考える - ラムダ式はどうやって動くのか
skrb
7
10k
Project Amberで変わる Javaのプログラミングスタイル
skrb
3
790
String Templateによる文字列補間
skrb
4
2.7k
Virtual Threadの動作と効果的な使い方
skrb
2
470
JVMLSに参加してきた
skrb
1
1.8k
Who's Who in Java
skrb
3
19k
Javaの並列/並行処理の基本
skrb
9
3.2k
Getting Started with Presentation
skrb
1
2.4k
Other Decks in Technology
See All in Technology
Flutter研修【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
160
AWSサービスメニュー開発をしていてAWSを好きだ!と感じた瞬間
toru_kubota
0
130
Docker互換のセキュアなコンテナ実行環境「Podman」超入門
devops_vtj
6
3.2k
コミュニティサービスに「あなたへ」フィードを リリースするまでの試行錯誤
takapy
1
140
頼られるのが大好きな 皆さんへ - 支援相手との期待の合わせ方、突き放し方 -/For_people_who_like_to_be_relied_on
naitosatoshi
1
290
ゆめみのアクセシビリティの現在地と今後
ryokatsuse
3
290
ソフトウェアエンジニアリングの知見を活かして データ基盤をいい感じにする on Snowflake [MIERUNE BBQ #10]
mtpooh
2
150
What is DRE? - Road to SRE NEXT@広島
chanyou0311
3
620
[2024最新版]AWS Control Towerを使ったセキュアなマルチアカウント環境の作り方
hiashisan
0
270
データベース研修 分析向けSQL入門【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
110
Git 研修 Basic【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
310
データ分析基盤を作ってみよう~設計編~
nrinetcom
PRO
1
110
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
662
120k
The Invisible Customer
myddelton
117
13k
Producing Creativity
orderedlist
PRO
340
39k
Building Adaptive Systems
keathley
34
2k
How To Stay Up To Date on Web Technology
chriscoyier
784
250k
For a Future-Friendly Web
brad_frost
173
9.2k
The Invisible Side of Design
smashingmag
294
50k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
26
1.8k
Designing the Hi-DPI Web
ddemaree
276
34k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
353
29k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
23
1.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
399
65k
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 • 可読性向上 • 関数の活用 • 状態変化を避けること • 新しい言語仕様/機能を積極的に試す を意識してコードを記述