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
3
5.1k
モダンなJavaの書き方
2021.02.25
TECH Street
Javaエンジニア勉強会資料
Yuichi.Sakuraba
March 01, 2021
Tweet
Share
More Decks by Yuichi.Sakuraba
See All by Yuichi.Sakuraba
Java 25に至る道
skrb
3
240
Lazy Constant - finalフィールドの遅延初期化
skrb
0
2k
Language Update: Java
skrb
2
400
Java 30周年記念! Javaの30年をふりかえる
skrb
4
3.5k
JavaにおけるNull非許容性
skrb
2
3.7k
あなたはJVMの気持ちを理解できるか?
skrb
6
30k
で、ValhallaのValue Classってどうなったの?
skrb
2
14k
Javaにおける関数型プログラミンへの取り組み
skrb
7
660
今こそ、ラムダ式を考える - なぜあなたはラムダ式を苦手と感じるのか
skrb
6
26k
Other Decks in Technology
See All in Technology
AI開発をスケールさせるデータ中心の仕組みづくり
kzykmyzw
0
170
AWS監視を「もっと楽する」ために
uechishingo
0
430
2026年はチャンキングを極める!
shibuiwilliam
7
1.1k
【northernforce#54】SalesforceにおけるAgentforceの位置づけ・事例紹介
yutosatou_kit
0
130
Regional_NAT_Gatewayについて_basicとの違い_試した内容スケールアウト_インについて_IPv6_dual_networkでの使い分けなど.pdf
cloudevcode
1
160
最速で価値を出すための プロダクトエンジニアのツッコミ術
kaacun
1
260
[Iceberg Meetup #4] ゼロからはじめる: Apache Icebergとはなにか? / Apache Iceberg for Beginners
databricksjapan
0
500
Git Training GitHub
yuhattor
1
270
3リポジトリーを2ヶ月でモノレポ化した話 / How I turned 3 repositories into a monorepo in 2 months
kubode
0
120
SwiftDataを覗き見る
akidon0000
0
310
EventBridge API Destination × AgentCore Runtimeで実現するLambdaレスなイベント駆動エージェント
har1101
7
260
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
41k
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
34
9.1k
Getting science done with accelerated Python computing platforms
jacobtomlinson
1
100
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
120
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.9k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
0
260
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
410
Optimising Largest Contentful Paint
csswizardry
37
3.6k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
380
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
170
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
900
Practical Orchestrator
shlominoach
191
11k
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 • 可読性向上 • 関数の活用 • 状態変化を避けること • 新しい言語仕様/機能を積極的に試す を意識してコードを記述