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
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
210
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
870
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
250
CSC305 Lecture 13
javiergs
PRO
0
130
現場で役立つモデリング 超入門
masuda220
PRO
13
2.9k
C#/.NETのこれまでのふりかえり
tomokusaba
1
160
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
430
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
1.7k
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
1.7k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
150
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
140
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
243
12k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
Building Applications with DynamoDB
mza
90
6.1k
Building an army of robots
kneath
302
42k
The Art of Programming - Codeland 2020
erikaheidi
51
13k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Designing for humans not robots
tammielis
249
25k
Documentation Writing (for coders)
carmenintech
65
4.4k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
328
21k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Writing Fast Ruby
sferik
626
61k
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ʹ༏͍͠ • ࢄɾฒߦॲཧΛӅṭԽ࣮ͯ͠Ͱ͖ͦ͏ͳ
ઃܭํΛ͍ࣔͯ͠Δ