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
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
みんなでプロポーザルを書いてみた
yuriko1211
0
280
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
cmp.Or に感動した
otakakot
3
200
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
900
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
CSC509 Lecture 09
javiergs
PRO
0
140
RubyLSPのマルチバイト文字対応
notfounds
0
120
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Amazon Qを使ってIaCを触ろう!
maruto
0
410
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Featured
See All Featured
Optimizing for Happiness
mojombo
376
70k
Writing Fast Ruby
sferik
627
61k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Visualization
eitanlees
145
15k
How GitHub (no longer) Works
holman
310
140k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Unsuck your backbone
ammeep
668
57k
The Cost Of JavaScript in 2023
addyosmani
45
6.8k
Bash Introduction
62gerente
608
210k
Being A Developer After 40
akosma
87
590k
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ʹ༏͍͠ • ࢄɾฒߦॲཧΛӅṭԽ࣮ͯ͠Ͱ͖ͦ͏ͳ
ઃܭํΛ͍ࣔͯ͠Δ