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
270
hpc170_slide.pdf
serihiro
0
41
画像解像度別ImageNetの100 iterationの合計処理時間比較
serihiro
0
89
深層ニューラルネットワークにおける訓練高速化のための自動最適化
serihiro
0
36
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
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
160
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
170
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4.1k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
430
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
480
Realtime API 入門
riofujimon
0
110
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
Vitest Browser Mode への期待 / Vitest Browser Mode
odanado
PRO
2
1.7k
EventSourcingの理想と現実
wenas
6
2.1k
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.1k
Vue.js学習の振り返り
hiro_xre
2
130
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
Featured
See All Featured
Optimizing for Happiness
mojombo
376
69k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Facilitating Awesome Meetings
lara
49
6k
Music & Morning Musume
bryan
46
6.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Code Reviewing Like a Champion
maltzj
519
39k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Designing for Performance
lara
604
68k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
GitHub's CSS Performance
jonrohan
1030
460k
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ʹ༏͍͠ • ࢄɾฒߦॲཧΛӅṭԽ࣮ͯ͠Ͱ͖ͦ͏ͳ
ઃܭํΛ͍ࣔͯ͠Δ