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
5分で分かるかもしれないjava8 Stream API
Search
Kazuhiro Serizawa
November 26, 2015
Programming
1
2.2k
5分で分かるかもしれないjava8 Stream API
Kazuhiro Serizawa
November 26, 2015
Tweet
Share
More Decks by Kazuhiro Serizawa
See All by Kazuhiro Serizawa
Accelerating Machine Learning I/O by Overlapping Data Staging and Mini-batch Generations
serihiro
1
280
hpc170_slide.pdf
serihiro
0
45
画像解像度別ImageNetの100 iterationの合計処理時間比較
serihiro
0
94
深層ニューラルネットワークにおける訓練高速化のための自動最適化
serihiro
0
41
Introduction to Parallel Computing 2.2
serihiro
0
51
My summer internship result at Treasure Data 2018 #td_intern
serihiro
0
2k
startupでもrails使うなら これだけはやっとけ的 tips集
serihiro
19
10k
つらくないコードレビューの運用
serihiro
43
20k
Other Decks in Programming
See All in Programming
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
ドメインイベント増えすぎ問題
h0r15h0
1
280
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
140
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
210
Security_for_introducing_eBPF
kentatada
0
110
php-conference-japan-2024
tasuku43
0
240
42 best practices for Symfony, a decade later
tucksaun
1
180
Go の GC の不得意な部分を克服したい
taiyow
2
770
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
330
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
66
4.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
A Philosophy of Restraint
colly
203
16k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Docker and Python
trallard
42
3.1k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Navigating Team Friction
lara
183
15k
4 Signs Your Business is Dying
shpigford
181
21k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Transcript
5Ͱ͔Δ͔͠Εͳ͍ java8 Stream API
java8ʹ͍ͭͯ • 20143݄18ʹOracle͔Βެ։ • “ੈքҰͷ։ൃϓϥοτϑΥʔϜͷըظతͳϦϦʔ ε” ※OracleେຊӦൃද • ϥϜμࣜɺStreamAPIɺDate and
Time APIͳͲͷ ৽ػೳͷಋೖͰjavaքʹֵ໋͕ʂ ʢͱࢥ͚ͬͨͲͦ͜·ͰΓ্͕Βͳ͔ͬͨʣ
ࠓ͔̑͠ͳ͍ͷͰ • Stream APIʹ͍ͭͯ֓ཁΛ͠·͢ • ʮjavaগ͠ϚγʹͳͬͨΑ͏ͩͳʯ ͱ͍͏ͷΛײ͡औͬͯΒ͑Ε͍Ͱ͢
Stream APIͱʁ • ྻɺϦετͳͲͷෳͷσʔλʹ͓͚Δू ૢ࡞ͷαϙʔτΛߦ͏API • ฒߦॲཧΒͷϕϯϦػೳΛఏڙͯ͘͠ΕΔ UtilͰͳ͘Interface͚ͩΛఏڙ͢Δ
Stream APIͱʁ • ཁ͢ΔʹRubyͰݴ͏ͱ͜Ζͷ • Enumerable#each • Enumerable#select • Enumerable#map
• ͜ΕΒͷॲཧΛʮܾ·ͬͨखॱͰ࣮ߦ͢ΔʯAPI • Stream API ≠࣮ɻInterfaceͷΈΛఆ͍ٛͯ͠Δɻ
StreamΛΘͳ͍߹ͷྫ List<Integer> list = Arrays.asList(1,2,3); Integer result = 0; for
(Integer i : list) { result += i * i; } System.out.println(result); //14
ͳ͓ruby result = [1,2,3] .map{ |i| i*i } .inject{ |a,b|
a + b } puts result #=> 14
StreamΛ͏߹ͷྫ List<Integer> list = Arrays.asList(1,2,3); Integer result = list.stream() .map(x
-> x * x) .reduce((a, b) -> a + b) .get(); System.out.println(result); //14
Stream APIΛ༻͍࣮ͨ "SSBZ -JTUͳͲͷ %BUBTPVSDF TUSFBN࡞ தؒૢ࡞ ϑΟϧλϦϯάɾͷૢ࡞ͳͲ ऴૢ࡞ ूૢ࡞ɾผΫϥεͷมɾ
ஞ࣍ॲཧͳͲ ॲཧ͞Εͨ݁Ռ list.stream() .map(x -> x * x) .reduce((a, b) -> a + b) .get();
ඪ४ϥΠϒϥϦ with Stream Stream<String> lines = Files.lines(path) lines.filter(line -> line.startsWith(“a”))
.map(s -> s.toUpperCase) .forEach(s -> System.out.println(s)); // “AMAZON” // “ATOM”
StreamʹΑͬͯԿ͕͏Ε͍͔͠ • σʔλϦιʔεૢ࡞ͷ࣮ͷ؆ུԽ • ϥϜμࣜͱΈ߹ΘͤΔ͜ͱͰ ؔܕͬΆ͍syntaxʹ • σʔλϦιʔεʹର͢Δฒߦॲཧɾࢄॲཧ Λstream()ϝιουʹӅṭԽ࣮ͯ͠Ͱ͖Δ
Collection frameworkͩͱ ϚϧνεϨουͰฒߦॲཧ͢ΔstreamΛ࡞ΕΔ List<Integer> list = Arrays.asList(1,2,3); Integer result =
list.parallelStream() .map(x -> x * x) .reduce((a, b) -> a + b) .get(); System.out.println(result); //14
༨ஊɿ Apache SparkΛͬͨࢄॲཧ࣮ input. flatMap(line -> Arrays.asList(line.split("\\s+"))) .mapToPair(word -> new
Tuple2(word, 1)) .reduceByKey((x, y) -> (Integer) x + (Integer) y) .sortByKey() .collect();
Stream APIͷ·ͱΊ • ීஈྑ͘͏Collection Frameworkͷॲཧ͕ γϯϓϧʹ࣮Ͱ͖ΔΑ͏ʹͳΔ • rubyʹ༏͍͠ • ࢄɾฒߦॲཧΛӅṭԽ࣮ͯ͠Ͱ͖ͦ͏ͳ
ઃܭํΛ͍ࣔͯ͠Δ