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
4.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
あなたはJVMの気持ちを理解できるか?
skrb
5
2k
で、ValhallaのValue Classってどうなったの?
skrb
2
8.3k
Javaにおける関数型プログラミンへの取り組み
skrb
7
420
今こそ、ラムダ式を考える - なぜあなたはラムダ式を苦手と感じるのか
skrb
6
21k
今こそ、ラムダ式を考える - ラムダ式はどうやって動くのか
skrb
7
11k
Project Amberで変わる Javaのプログラミングスタイル
skrb
3
970
String Templateによる文字列補間
skrb
4
3.6k
Virtual Threadの動作と効果的な使い方
skrb
2
580
JVMLSに参加してきた
skrb
1
2k
Other Decks in Technology
See All in Technology
private spaceについてあれこれ調べてみた
operando
1
170
Server Side Swift 実践レポート: 2024年に案件で採用して見えた課題と可能性
yusuga
1
420
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
10
120k
CNAPPから考えるAWSガバナンスの実践と最適化
nrinetcom
PRO
1
330
GraphRAG: What I Thought I Knew (But Didn’t)
sashimimochi
1
230
プロダクト価値を引き上げる、「課題の再定義」という習慣
moeka__c
0
210
プロダクト観点で考えるデータ基盤の育成戦略 / Growth Strategy of Data Analytics Platforms from a Product Perspective
yamamotoyuta
0
250
ソフトウェアアーキテクトのための意思決定術: Software Architecture and Decision-Making
snoozer05
PRO
17
4k
企業テックブログにおける執筆ネタの考え方・見つけ方・広げ方 / How to Think of, Find, and Expand Writing Topics for Corporate Tech Blogs
honyanya
0
820
Makuake*UPSIDER_LightningTalk
upsider_tech
0
210
EDRからERM: PFN-SIRTが関わるセキュリティとリスクへの取り組み
pfn
PRO
0
110
Agentic AI時代のプロダクトマネジメントことはじめ〜仮説検証編〜
masakazu178
3
410
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
192
16k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
520
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
19k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
990
How GitHub (no longer) Works
holman
312
140k
Scaling GitHub
holman
459
140k
Speed Design
sergeychernyshev
25
760
Mobile First: as difficult as doing things right
swwweet
222
9.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
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 • 可読性向上 • 関数の活用 • 状態変化を避けること • 新しい言語仕様/機能を積極的に試す を意識してコードを記述