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
Kotlin Collection関数をマスター
Search
Masayuki Suda
June 22, 2024
Programming
1
4.9k
Kotlin Collection関数をマスター
Kotlin Fest 2024にて登壇させていただいた資料です。
KotlinのCollection関数を、たくさん紹介させていただきました。
Masayuki Suda
June 22, 2024
Tweet
Share
More Decks by Masayuki Suda
See All by Masayuki Suda
Kotlin 2.0が与えるAndroid開発の進化
masayukisuda
2
1.4k
Jetpack Macrobenchmarkを試してみた
masayukisuda
1
260
MediaCodecで動画編集をしてみよう
masayukisuda
3
4.5k
Androidで動画編集アプリの技術選択
masayukisuda
1
1.3k
Other Decks in Programming
See All in Programming
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
Amazon Qを使ってIaCを触ろう!
maruto
0
410
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
140
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
930
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Designing the Hi-DPI Web
ddemaree
280
34k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Bash Introduction
62gerente
608
210k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Gamification - CAS2011
davidbonilla
80
5k
Transcript
Kotlin CollectionؔΛϚελʔ 2024.06.22 Masayuki Suda © LINE Digital Frontier Corporation
ύϑΥʔϚϯεͱՄಡੑΛཱ྆
࣍ 01. ΠϯτϩμΫγϣϯ 1.1 ࣗݾհ 1.2 ͖͔͚ͬ 04. ؔͷհ2 4.1
෦औಘ 4.2 ୯Ұཁૉऔಘ 05. ߴͳؔͷ༻ྫ 5.1 chunked 5.2 windowed 02. Kotlin Collectionͷ֓ཁ 2.1 ϑϨʔϜϫʔΫجຊߏ 2.2 ओཁͳΠϯλʔϑΣʔε 03. ؔͷհ1 3.1 ม 3.2 ϑΟϧλϦϯά 06. ύϑΥʔϚϯεͷ࠷దԽ 6.1 Sequenceͱforจ 6.2 ·ͱΊ 3.3 ཁૉͷՃɾআ 3.4 άϧʔϓԽ 4.3 ฒସ͑ 4.4 ूؔ
ΠϯτϩμΫγϣϯ 01. ࣗݾհ ͖͔͚ͬ
1.1 ࣗݾհ LINE Digital Frontier גࣜձࣾͱ
1.2 ͖͔͚ͬ ɾKotlin Collectionͷ֦ுؔͬͯͷ͍͕͋͢͝Δ͚Ͳ͋·ΓΒͳ͍ ɾશ෦͓͞Β͍͢Δػձ͕͋Δͱخ͍͠ͳ
Kotlin Collectionͷ֓ཁ ϑϨʔϜϫʔΫͷ֓ཁ ओཁͳΠϯλʔϑΣʔε 02.
2.1 ϑϨʔϜϫʔΫجຊߏ ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638
2.1 ϑϨʔϜϫʔΫجຊߏ ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638
2.1 ϑϨʔϜϫʔΫجຊߏ Iterable IterableͱɺίϨΫγϣϯͷཁૉΛॱ൪ʹऔಘͰ͖ΔΑ͏ʹ͢ΔͨΊͷجຊతͳΠϯ λʔϑΣʔε
hasNext(): ίϨΫγϣϯʹ࣍ͷཁૉ͕ଘࡏ͢Δ߹trueΛฦ͠·͢ɻ next(): ίϨΫγϣϯͷ࣍ͷཁૉΛฦ͠·͢ɻ Iterator 2.1 ϑϨʔϜϫʔΫجຊߏ
2.1 ϑϨʔϜϫʔΫجຊߏ MutableIterator remove(): ݱࡏͷཁૉΛίϨΫγϣϯ͔Βআ͠·͢
2.2 ओཁͳΠϯλʔϑΣʔε ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638
2.2 ओཁͳΠϯλʔϑΣʔε List ॱং͚͞ΕͨཁૉͷίϨΫγϣϯͰɺॏෳཁૉΛؚΉ͜ͱ͕Մೳ ΠϯσοΫεΛ༻ͯ͠ΞΫηεͰ͖Δ
List
2.2 ओཁͳΠϯλʔϑΣʔε Set ॱং͚͞Εͳ͍ཁૉͷίϨΫγϣϯͰɺॏෳཁૉΛؚ·ͳ͍
2.2 ओཁͳΠϯλʔϑΣʔε Set
2.2 ओཁͳΠϯλʔϑΣʔε Map ɾΩʔͱͷϖΞͷίϨΫγϣϯͰɺ֤ΩʔҰҙ ɾListSetͱҧͬͯɺCollectionΠϯλʔϑΣΠεΛʹܧঝͤͣʹɺಠࣗͰMapΠϯλʔϑΣ ΠεΛ࣋ͭ
2.2 ओཁͳΠϯλʔϑΣʔε Map
2.2 ओཁͳΠϯλʔϑΣʔε ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638
2.2 ओཁͳΠϯλʔϑΣʔε Immutable Mutable ɾKotlinͷίϨΫγϣϯɺimmutableͱmutableͷόʔδϣϯ͕͋Γ·͢ ɾimmutableมߋෆՄɺmutableมߋՄೳ ɾmutableʹimmutableʹଘࡏ͠ͳ͍add, removeͱ͍ͬͨมߋͰ͖Δ͕ؔ༻ҙ͞Ε͍ͯ·͢
ؔͷհ1 มɺϑΟϧλϦϯά ཁૉͷՃɾআɺάϧʔϓԽ 03.
Collectionͷجຊૢ࡞Λߦ͏ϝϯόʔؔΛఏڙ͠·͢ɻྫ͑ɺCollectionͷ isEmpty()Listͷget()͕͋Γ·͢ɻಠࣗͷίϨΫγϣϯΛ࣮͢Δࡍʹɺ͜Ε Βͷϝϯόʔ࣮͕ؔඞཁͰ͢ɻඪ४ϥΠϒϥϦʹɺ৽͍͠ίϨΫγϣϯ Λ؆୯ʹ࡞͢ΔͨΊͷࠎ࣮֨༻ҙ͞Ε͍ͯ·͢ɻ CollectionͷϑΟϧλϦϯάɺมɺฒସ͑ͳͲΛߦ͏֦ுؚؔ·Ε͍ͯ· ͢ɻ֦ுؔΛ͏͜ͱͰɺطଘͷίϨΫγϣϯʹ৽͍͠ػೳΛॊೈʹՃͰ͖ ·͢ɻ ϝϯόʔؔ ֦ுؔ
3.1 ม طଘͷCollection͔Β৽͍͠CollectionΛߏங͠·͢ طଘͷίϨΫγϣϯ͔Β৽͍͠ίϨΫγϣϯΛߏங͠·͢ Map ผͷCollectionͷཁૉʹର͢Δؔͷ݁Ռ͔ΒCollectionΛ࡞ ͠·͢ Zip ྆ํͷCollectionͷಉ͡Ґஔʹ͋Δཁૉ͔ΒϖΞΛߏங͠· ͢
Associate CollectionཁૉͱͦΕΒʹؔ࿈͚ΒΕͨಛఆͷ͔ΒϚοϓ ΛߏஙͰ͖·͢ Flatten ωετ͞ΕͨCollectionΛҰͭͷCollectionʹ͠·͢
3.1 ม Map ɾCollectionͷ֤ཁૉʹࢦఆͨ͠มؔΛద༻͠ɺͦͷ݁Ռ͔Β৽͍͠CollectionΛ࡞͠·͢ɻ ɾجຊతͳϚοϐϯάؔmap()Ͱ͢ɻ ɾࢦఆ͞ΕͨϥϜμؔΛ֤ཁૉʹద༻͠ɺϥϜμ݁ՌͷϦετΛฦ͠·͢ɻ ɾ݁Ռͷॱংݩͷཁૉͷॱংͱಉ͡Ͱ͢ɻ map() Signature Ex.
3.1 ม Map ɾཁૉΠϯσοΫεΛҾͱͯ͠༻͢ΔมʹmapIndexed()Λ༻͠·͢ɻ mapIndexed() Signature Ex.
3.1 ม ɾมʹΑΓಛఆͷཁૉͰnull͕ੜ͞ΕΔ߹ ɾmapNotNull()Λ༻͢Δ͜ͱͰɺ݁ՌͷCollection͔ΒnullΛআ֎Ͱ͖·͢ɻ Map mapNotNull() Signature Ex.
3.1 ม ɾཁૉΠϯσοΫεΛҾͱͯ͠༻͢ΔࡍɺมʹΑΓಛఆͷཁૉͰnull͕ੜ͞ΕΔ߹ ɾmapIndexedNotNull()Λ༻͢Δ͜ͱͰɺ݁ՌͷCollection͔ΒnullΛআ֎Ͱ͖·͢ɻ Map mapIndexedNotNull() Signature Ex.
3.1 ม ɾCollectionͷ֤ཁૉʹରͯ͠ࢦఆ͞ΕͨมؔΛద༻͠ɺ݁ՌΛࢦఆ͞ΕͨѼઌCollectionʹ Ճ͢Δ ɾ৽͍͠CollectionΛੜ͢ΔͷͰͳ͘ɺطଘͷCollectionʹม݁ՌΛՃ͠·͢ɻ ɾmapIndexedTo(), mapNotNullTo()ͳͲɺଞͷmapܥؔશͯʹto͕͍͍ͭͯ·͢ɻ Map mapTo() Signature
Ex.
3.1 ม Zip ɾZipมɺೋͭͷCollectionͷಉ͡Ґஔʹ͋Δཁૉ͔ΒϖΞΛߏங͠·͢ɻ ɾ݁ՌͷϖΞͷ࠷ॳͷཁૉɺReceiver CollectionͷཁૉͰ͢ɻ ɾCollectionͷαΠζ͕ҟͳΔ߹ɺ݁Ռখ͍͞ํͷαΠζʹͳΓ·͢ɻ ɾzip()ɺa zip bܗࣜͷதஔͰݺͼग़ͤ·͢ɻ
ɾܭࢉྔO(min(n, m)) Signature zip() Ex.
3.1 ม ɾzip()2ͭͷύϥϝʔλʢड৴ଆཁૉͱҾཁૉʣΛऔΔมؔΛ༻ͯ͠ݺͼग़͢͜ͱͰ͖·͢ɻ ɾ͜ͷ߹ɺ݁ՌϦετʹɺड৴ଆཁૉͱҾཁૉͷϖΞʹରͯ͠มؔΛద༻ͨ͠Γؚ͕·Ε·͢ɻ Zip zip() Ex. Signature
3.1 ม ɾϖΞͷϦετ͕͋Δ߹ɺ͜ΕΒͷϖΞ͔Β 2 ͭͷϦετΛߏங͢Δٯม (ղౚ) Λ࣮ߦͰ͖·͢ɻ Zip unzip() Signature
Ex.
3.1 ม Associate ɾCollectionΛMapʹม͢ΔͨΊͷศརͳؔͰ͢ associateWith() Signature Ex.
3.1 ม Associate ɾassociateByؔͰɺCollectionͷ֤ཁૉ͕݁ՌͷϚοϓͰͲͷΑ͏ʹΩʔͱͷϖΞʹม͞ΕΔ͖ ͔Λࢦఆ͢Δ͜ͱ͕Ͱ͖·͢ associateBy() Signature Ex.
3.1 ม Associate ɾ৽͍͠CollectionΛੜ͢ΔͷͰͳ͘ɺطଘͷMapʹཁૉΛՃ͠·͢ɻ ɾassociateByTo(), associateWithTo()ͳͲɺଞͷassociateܥؔશͯʹto͕͍͍ͭͯ·͢ɻ associateTo() Signature Ex.
3.1 ม Associateɹ ҙ ɾPair ObjectΛଟੜͯ͠͠·͏͜ͱͷݒ೦ ɹɾ֤Collectionཁૉʹର໋ͯ͠ͷPair ObjectΛੜ͠·͢ɻ͜ΕʹΑΓɺҰ࣌తͳϝϞϦ༻ྔ͕૿Ճ͠·͢ɻ ɹɾେنͳCollectionʹରͯ͠associate()Λ༻͢ΔͱɺଟͷPair Object͕࡞͞ΕɺΨϕʔδίϨΫγϣϯͷ
ෛ୲͕૿Ճ͢ΔՄೳੑ͕͋Γ·͢ɻ ɹɾPair ObjectΛੜ͢Δίετ͕͔͔Γ·͢ɻ֤ཁૉʹରͯ͠ϖΞΛ࡞͢ΔͨΊɺଟͷObjectੜ͕ൃੜ͠ ·͢ɻ ɹɾObjectੜͷΦʔόʔϔου͕ྦྷੵ͢ΔͱɺύϑΥʔϚϯεʹӨڹΛ༩͑Δ͜ͱ͕͋Γ·͢ɻ ɾύϑΥʔϚϯε͕ॏཁͰͳ͍߹ɺΩʔͱΛҰॹʹੜ͢Δඞཁ͕͋Δ߹ʹద͍ͯ͠·͢ɻ
3.1 ม Flatten ɾωετ͞ΕͨCollectionͷͯ͢ͷཁૉͷ୯ҰͷϦετΛฦ͠·͢ɻ Signature Ex. flatten()
Flatten 3.1 ม Signature Ex. flatMap() ɾCollectionͷ֤ཁૉʹରͯ͠ࢦఆ͞ΕͨมؔΛద༻͠ɺͦͷ݁Ռͱͯ͠ಘΒΕͨCollectionΛ Ұͭʹ·ͱΊͯฏୱԽ͠·͢ɻ ɾܭࢉྔO(n +
m) ɹɾnݩͷίϨΫγϣϯͷཁૉɻ ɹɾmม͞ΕͨίϨΫγϣϯͷશཁૉͷ߹ܭɻ ɾΦʔόʔϔου ɹɾ֤ཁૉʹରͯ͠ϦετΛੜ͢ΔͨΊɺෆཁͳΦʔόʔϔου͕ൃੜ͢ΔՄೳੑ͕͋Γ·͢ɻ
3.2 ϑΟϧλϦϯά طଘͷCollectionΛϑΟϧλʔ͠ɺ৽͍͠CollectionΛߏங͠·͢ filter(), filterNot() جຊతͳϑΟϧλϦϯάɻ݅ʹ߹க͢Δ͔ɺ͠ͳ͍͔ͰϑΟϧλϦ ϯά͠·͢ɻ filterIndexed() Indexͱʹج͍ͮͯཁૉΛϑΟϧλϦϯά͠·͢ɻ filterIsInstance(),
filterNotNull() ಛఆͷܕͷཁૉ ɺnullΛߟྀͯ͠ϑΟϧλϦϯά͠·͢ɻ partition() ݅ʹ߹க͢Δཁૉͱ͠ͳ͍ཁૉΛ2ͭͷϦετʹ͚·͢ɻ any(), none(), all() Collectionͷཁૉ͕݅ʹ߹க͢Δ͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ filterTo() طଘͷCollectionʹϑΟϧλϦϯά݁ՌΛՃ͠·͢ɻ
3.2 ϑΟϧλϦϯά filter() Signature Ex. ɾ݅ʹ߹க͢ΔཁૉΛϦετͰฦ͠·͢ɻ
3.2 ϑΟϧλϦϯά filterNot() Signature Ex. ɾ݅ʹ߹க͠ͳ͍ཁૉΛϦετͰฦ͠·͢ɻ
3.2 ϑΟϧλϦϯά filterIndexed() Signature Ex. ɾΠϯσοΫεͱʹج͍ͮͯཁૉΛϑΟϧλϦϯά͠·͢ɻ
3.2 ϑΟϧλϦϯά filterIsInstance() Signature Ex. ɾಛఆͷܕͷཁૉΛϑΟϧλϦϯά͠·͢ɻ
3.2 ϑΟϧλϦϯά filterNotNull() Signature Ex. ɾnullҎ֎ͷཁૉΛϑΟϧλϦϯά͠·͢ɻ
3.2 ϑΟϧλϦϯά filterTo() Signature Ex. ɾطଘͷCollectionʹϑΟϧλϦϯά݁ՌΛՃ͠·͢ɻ ɾfilterNotTo(), filterNotNullTo(), filterIsInstanceTo(), filterIndexedTo(),ଘࡏ͢Δ
partition() Signature Ex. 3.2 ϑΟϧλϦϯά ɾ݅ʹ߹க͢Δཁૉͱ͠ͳ͍ཁૉΛ2ͭͷϦετʹ͚·͢ɻ
3.2 ϑΟϧλϦϯά any() Signature Ex. ɾCollectionͷཁૉ͕݅ʹ߹க͢Δͷ͕͋Δ͔ΛνΣοΫ͠·͢ɻ
3.2 ϑΟϧλϦϯά all() Signature Ex. ɾCollectionͷཁૉ͕݅ʹશͯ߹க͢Δ͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ
3.2 ϑΟϧλϦϯά none() Signature Ex. ɾCollectionͷཁૉ͕݅ʹશͯ߹க͍ͯ͠ͳ͍͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ
3.3 ཁૉͷՃɾআ Collectionʹରͯ͠+ʢplusʣͱ-ʢminusʣԋࢉࢠ͕ఆٛ͞Ε͍ͯ·͢ɻ ͜ΕΒ৽͍͠ಡΈऔΓઐ༻ͷCollectionΛฦ͠·͢ɻ Ex. ※ ৽͘͠listΛੜ͢ΔɺϝϞϦΛফඅ͠·͢ɻϝϞϦ͕ݫ͍͠߹ mutableListΛ͏ݕ౼ͳͲ͠·͠ΐ͏ɻ
3.4 άϧʔϓԽ groupBy() ࢦఆͨ͠ΩʔબؔʹΑͬͯཁૉΛάϧʔϓԽ͠ɺϚοϓͱͯ͠ฦ ͠·͢ɻ groupingBy() άϧʔϓԽΛԆ࣮ߦ͢ΔGroupingΠϯελϯεΛฦ͠ɺ eachCount()ɺfold()ɺreduce(), aggregate()ͳͲͷૢ࡞Λαϙʔτ͠ ·͢ɻ
3.4 άϧʔϓԽ Signature Ex. groupBy() ɾطଘͷCollectionΛࢦఆ͞ΕͨΩʔʹج͍ͮͯάϧʔϓԽ͠ɺMapͱͯ͠ฦ͠·͢ɻ
3.4 άϧʔϓԽ Signature Ex. groupBy() ɾطଘͷCollectionΛࢦఆ͞ΕͨΩʔʹج͍ͮͯάϧʔϓԽ͠ɺvalueTransformͰݩͷཁૉΛมߋ͠Mapͱͯ͠ฦ͠ ·͢ɻ
3.4 άϧʔϓԽ groupingBy() Signature ɾԆධՁΛߦ͍ɺதͰGroupingΦϒδΣΫτΛੜ͍ͯ͠·͢ɻऴॲཧͰMapΛฦ͠·͢ɻ
3.4 άϧʔϓԽ groupingBy() Signature ɾgroupingByΛ࣮ߦ͚ͨͩ͠ͰάϧʔϓԽى͜Γ·ͤΜɻeachCount()ɺfold()ɺreduce(), aggregate()ͳͲͷऴؔΛ࣮ߦͯ͠ॳΊͯgroupingԽ͞Ε·͢ɻ Ex.
3.4 άϧʔϓԽ groupingBy{}.eachCount() Signature Ex. ɾ֤άϧʔϓͷཁૉΛΧϯτ͢ΔϝιουͰ͢ɻ ɾgroupingByͰάϧʔϓԽͨ͠ޙʹeachCountΛݺͼग़͢ͱɺ֤άϧʔϓͷΩʔͱͦͷཁૉΛϚοϓ ͱͯ͠ฦ͠·͢
groupingBy{}.reduce() 3.4 άϧʔϓԽ Signature Ex. ɾάϧʔϓ͝ͱͷཁૉΛྦྷੵ͠ɺ୯Ұͷʹू͢ΔͨΊʹ༻͞Ε·͢ɻ ɾ֤άϧʔϓͷ࠷ॳͷཁૉΛॳظͱ͠ɺࢦఆͨ͠ૢ࡞Λద༻͠·͢ɻ
groupingBy{}.fold() 3.4 άϧʔϓԽ Signature Ex. ɾ֤άϧʔϓʹରͯ͠ྦྷੵૢ࡞Λߦ͏ͨΊʹ༻͞Ε·͢ɻ ɾॳظͱྦྷੵؔΛࢦఆͯ͠ɺ֤άϧʔϓͷཁૉΛॱ࣍ॲཧ͠ɺྦྷੵ݁ՌΛੜ͠·͢ɻ
groupingBy{}.aggregate() 3.4 άϧʔϓԽ Signature Ex. ɾάϧʔϓ͝ͱʹΧελϜूܭૢ࡞Λߦ͏ͨΊͷؔͰ͢ɻ ɾreducefoldͰ࣮ݱ͕͍͠߹ͷΧελϜૢ࡞ͱͯ͑͠·͢ɻ
groupingBy{} .aggregate() 3.4 άϧʔϓԽ
3.4 άϧʔϓԽ groupByTo() ɾطଘͷMapʹάϧʔϓԽ͞ΕͨཁૉΛՃ͠·͢ɻ ɾgroupingByͷeachCount, fold, reduce, aggregateʹToϝιουଘࡏ͢Δ Signature Ex.
4.1 ෦औಘ slice() ࢦఆͨ͠ΠϯσοΫεͷϦετΛجʹ৽͍͠ϦετΛ࡞͠·͢ɻ take() Ϧετͷઌ಄͔Βࢦఆͨ͠ͷཁૉΛऔಘ͠·͢ɻ drop() Ϧετͷઌ಄͔Βࢦఆͨ͠ͷཁૉΛআ֎ͨ͠ΓΛऔಘ͠·͢ɻ chunked() ϦετΛࢦఆͨ͠αΠζͷνϟϯΫʢখ͞ͳ෦ʣʹׂ͠·͢ɻ
windowed() ࢦఆͨ͠αΠζͷΟϯυʢεϥΠσΟϯά෦ʣΛϦετ͔Β ੜ͠·͢ɻ طଘͷCollection͔ΒҰ෦ΛऔΓग़͠ɺsublistΛੜ͠·͢ɻ
4.1 ෦औಘ slice() Signature Ex. ɾCollectionͷཁૉ͕݅ʹશͯ߹க͢Δ͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ ɾܭࢉྔO(n) ɾࢦఆͨ͠ΠϯσοΫεͷϦετΛجʹ৽͍͠ϦετΛ࡞͠·͢ɻ ɾҾIntRangeͱIterableͷ̎ύλʔϯ͋Γ·͢ɻ
4.1 ෦औಘ take() takeLast() Signature Ex. ɾϦετͷઌ಄͔Βࢦఆͨ͠ͷཁૉΛऔಘ͠·͢ɻ ɾtakeLastϦετͷඌ͔Βࢦఆͨ͠ͷཁૉΛऔಘ͠·͢ɻ
4.1 ෦औಘ takeWhile() takeLastWhile() Signature Ex. ɾࢦఆ͞Ε͕ͨ݅ਅͰ͋ΔݶΓɺCollectionͷཁૉΛઌ಄͔Βॱʹऔಘ͠·͢ɻ ɾtakeLastࢦఆ͞Ε͕ͨ݅ਅͰ͋ΔݶΓɺCollectionͷཁૉΛඌ͔Βॱʹऔಘ͠·͢ɻ
4.1 ෦औಘ Signature Ex. drop() dropLast() ɾϦετͷઌ಄͔Βࢦఆͨ͠ͷཁૉΛআ֎ͨ͠ΓΛऔಘ͠·͢ɻ ɾdropLastϦετͷඌ͔Βࢦఆͨ͠ͷཁૉΛআ֎ͨ͠ΓΛऔಘ͠·͢ɻ
4.1 ෦औಘ dropWhile() dropLastWhile() Signature Ex. ɾࢦఆ͞Ε͕ͨ݅ਅͰ͋ΔݶΓɺCollectionͷཁૉΛઌ಄͔Βॱʹআ֎͠·͢ɻ ɾdropLastࢦఆ͞Ε͕ͨ݅ਅͰ͋ΔݶΓɺCollectionͷඌ͔ΒॱʹཁૉΛআ֎͠·͢ɻ
chunked() 4.1 ෦औಘ Signature Ex. ɾCollectionΛࢦఆͨ͠αΠζͷνϟϯΫʢখ͞ͳ෦ʣʹׂ͠·͢ɻ ɾνϟϯΫͨ͠ޙʹɺtransform͢ΔࣄՄೳͰ͢ ɾେྔͷσʔλΛҰʹॲཧ͢ΔͷͰͳ͘ɺগͣͭ͠ޮతʹॲཧ͍ͨ࣌͠ʹ˕
4.1 ෦औಘ windowed() Signature Ex. ɾࢦఆͨ͠αΠζͷΟϯυʢεϥΠσΟϯά෦ʣΛϦετ͔Βੜ͠·͢ ɾׂͨ͠ޙʹɺtransform͢ΔࣄՄೳͰ͢ ɾܭࢉྔ: O(n *
k) ʢnݩͷϦετͷαΠζɺkΟϯυͷαΠζʣ
4.1 ෦औಘ windowed() ɾؔͷ໊લɺwindowedͷ֓೦ɺ࿈ଓత·͍ͨσʔλγʔέϯεΛখ͞ͳʮΟϯυʯʹׂɻ ɾͦΕͧΕͷΟϯυʢࠓ૭͔Βݟ͍͑ͯΔ෦ʣʹରͯ͠ݸผʹॲཧΛߦ͏ͱ͍͏ํ๏ʹج͍͍ͮͯ·͢ɻ ग़యɹhttps://www.baeldung.com/kotlin/collections-api
4.2 ୯Ұཁૉऔಘ first() Collectionͷ࠷ॳͷཁૉΛऔಘ last() Collectionͷ࠷ޙͷཁૉΛऔಘ elementAt() ࢦఆ͞ΕͨindexͷཁૉΛऔಘ
4.1 ෦औಘ Signature Ex. first() ɾCollectionͷ࠷ॳͷཁૉΛऔಘ͠·͢ɻ݅Λࢦఆ͢Δ͜ͱՄೳͰ͢ɻ ɾແ͚ΕNoSuchElementException͕throw͞ΕΔͷͰҙ
4.1 ෦औಘ Signature Ex. last() ɾCollectionͷ࠷ޙͷཁૉΛऔಘ͠·͢ɻ݅Λࢦఆ͢Δ͜ͱՄೳͰ͢ɻ ɾແ͚ΕNoSuchElementException͕throw͞ΕΔͷͰҙ
4.1 ෦औಘ Signature Ex. firstOrNull() ɾCollectionͷ࠷ॳͷཁૉΛऔಘ͠·͢ɻ݅Λࢦఆ͢Δ͜ͱՄೳͰ͢ɻ ɾແ͚Εnull͕ฦ͖ͬͯ·͢ɻ
å 4.1 ෦औಘ Signature Ex. lastOrNull() ɾCollectionͷ࠷ޙͷཁૉΛऔಘ͠·͢ɻ݅Λࢦఆ͢Δ͜ͱՄೳͰ͢ɻ ɾແ͚Εnull͕ฦ͖ͬͯ·͢ɻ
4.1 ෦औಘ Signature Ex. firstNotNullOf() ɾCollectionͷཁૉΛࢦఆ͞ΕͨมؔΛద༻͠ɺ࠷ॳͷඇnullͷ݁ՌΛฦ͠·͢ɻ ɾແ͚ΕNoSuchElementException͕throw͞ΕΔͷͰҙ
4.1 ෦औಘ Signature Ex. firstNotNullOfNull() ɾCollectionͷཁૉΛࢦఆ͞ΕͨมؔΛద༻͠ɺ࠷ॳͷඇnullͷ݁ՌΛฦ͠·͢ɻ ɾແ͚Εnull͕ฦ͖ͬͯ·͢
4.1 ෦औಘ elementAt() elementAtOrNull() elementAtOrElse() ɾࢦఆ͞ΕͨindexͷཁૉΛऔಘ͠·͢ɻ ɾelementAt()ࢦఆ͞Εͨindex͕listͷൣғ֎ͩͬͨ߹IndexOutOfBoundsException͕throw ɾelementAtOrNull()ࢦఆ͞Εͨindex͕listͷൣғ֎ͩͬͨ߹null ɾelementAtOrElse()ࢦఆ͞Εͨindex͕listͷൣғ֎ͩͬͨ߹ࢦఆͨ͠default
4.3 ฒͼସ͑ sorted() ࣗવॱংʹج͍ͮͯཁૉΛιʔτ͠·͢ sortedDescending() ࣗવॱংʹج͍ͮͯཁૉΛ߱ॱʹιʔτ͠·͢ sortedBy() ࢦఆͨ͠ηϨΫλʔؔʹج͍ͮͯཁૉΛιʔτ͠·͢ sortedByDescending() ࢦఆͨ͠ηϨΫλʔؔʹج͍ͮͯཁૉΛ߱ॱʹιʔτ͠
·͢ sortedWith() ࢦఆͨ͠ίϯύϨʔλʹج͍ͮͯཁૉΛιʔτ͠·͢ reversed() ཁૉͷॱংΛసͤ͞·͢ shuffled() ཁૉΛϥϯμϜʹฒସ͑·͢
sorted() sortedDescending() ɾsorted()ঢॱʹsort͠·͢ ɾsortedDescending()߱ॱʹsort͠·͢ 4.3 ฒͼସ͑
sortedBy() sortedByDescending() ɾsortedByؔɺࢦఆͨ͠ϓϩύςΟΛج४ʹCollectionͷཁૉΛঢॱʹsort͠·͢ɻ ɾsortedByDescendingؔɺࢦఆͨ͠ϓϩύςΟΛج४ʹίϨΫγϣϯͷཁૉΛ߱ॱʹsort͠·͢ɻ 4.3 ฒͼସ͑
sortedWith() ɾsortedWithؔɺࢦఆͨ͠ComparableΛͬͯίϨΫγϣϯͷཁૉΛsort͠·͢ɻ ɾ͜ͷαϯϓϧͰɺcompareByͱthenByΛͬͯComparableΛ࡞͠ɺsortedWithؔ ʹ͍ͯ͠·͢ɻ·ͣɺ໊લΛج४ʹsort͠ɺ࣍ʹྸΛج४ʹsort͍ͯ͠·͢ɻ 4.3 ฒͼସ͑
reserved() asReserved() ɾreserved()asReserved()originalͷCollectionΛมߋͤͣʹٯॱʹฒସ͑ͨΛฦ͠·͢ɻ ɾreserved()৽͍͠ϦετΛฦ͠ɺasReserved()ݩͷϦετΛࢀরͯ͠࡞͢ΔͨΊɺ৽͍͠Ϧετ ࡞͠ͳ͍ ɹɾCollectionͷن͕େ͖͍ͱasReserved()ͷํ͕ϝϞϦޮɺ࣮ߦ͕ૣ͍߹͕͋Γ·͢ɻ 4.3 ฒͼସ͑
asReserved() ɾasReservedࢀরͷindexΛٯॱʹ͍ͯ͠Δ͚ͩͰ࣮ݱ͍ͯ͠·͢ɻ৽͍͠list࡞͠ͳ͍ɻ 4.3 ฒͼସ͑
shuffled() 4.3 ฒͼସ͑ ɾϦετͷཁૉΛϥϯμϜʹγϟοϑϧͯ͠৽͍͠ϦετΛ࡞͠·͢ɻݩͷϦετมߋ͞Ε·ͤΜɻ ɾϥϯμϜγʔυΛࢦఆͯ͠γϟοϑϧΛߦ͏͜ͱͰɺ࠶ݱՄೳͳγϟοϑϧ݁ՌΛಘΔ͜ͱͰ͖·͢ɻ
4.4 ूؔ maxOrNull() CollectionͷཁૉͷதͰ࠷େͷͷΛฦ͠·͢ minOrNull() CollectionͷཁૉͷதͰ࠷খͷͷΛฦ͠·͢ sum() CollectionͷཁૉΛ߹ܭ͢ΔͨΊʹ༻͞Ε·͢ average() CollectionͷཁૉͷฏۉΛܭࢉ͢ΔͨΊʹ༻͞Ε·͢
count() CollectionͷཁૉͷΛ͑ΔͨΊʹ༻͞Ε·͢ fold() ॳظͱྦྷੵૢ࡞Λࢦఆͯ͠ࠨ͔ΒӈʹίϨΫγϣϯΛંΓͨͨΉɻ reduce() ॳظͳ͠Ͱ࠷ॳͷཁૉΛॳظͱͯ͠ࠨ͔ΒӈʹίϨΫγϣϯΛॖখ͢ Δɻ
maxOrNull() ɾCollectionͷཁૉͷதͰ࠷େͷཁૉΛฦ͠·͢ɻ ɾCollectionͷཁૉ͕ແ͚ΕnullΛฦ͠·͢ɻ ɾmax()ؔඇਪͱͳͬͯ·͢ɻ 4.4 ूؔ
maxByOrNull() ɾΧελϜΦϒδΣΫτͷϓϩύςΟΛج४ʹͯ͠࠷େΛ؆୯ʹऔಘͰ͖·͢ɻ 4.4 ूؔ
4.4 ूؔ minOrNull() ɾCollectionͷཁૉͷதͰ࠷খͷཁૉΛฦ͠·͢ɻ ɾCollection͕ແ͚ΕnullΛฦ͠·͢ɻ ɾmin()ؔඇਪͱͳͬͯ·͢ɻ
4.4 ूؔ minByOrNull() ɾΧελϜΦϒδΣΫτͷϓϩύςΟΛج४ʹͯ͠࠷খΛ؆୯ʹऔಘͰ͖·͢ɻ
4.4 ूؔ sum() ɾCollectionͷཁૉͷ߹ܭΛฦ͠·͢ɻ
4.4 ूؔ sumOf() ɾΧελϜΦϒδΣΫτͷϓϩύςΟΛج४ʹͨ͠߹ܭͷऔಘ͕Ͱ͖·͢ɻ
4.4 ूؔ average() ɾCollectionͷͷฏۉΛܭࢉ͢ΔͨΊʹ༻͞Ε·͢ɻ ɾۭͷCollectionͷ߹ɺaverage()ؔNaNʢNot a NumberʣΛฦ͠·͢ɻ
4.4 ूؔ count() ɾCollectionͷཁૉͷΛऔಘ ɾಛఆͷ݅Λຬͨ͢ཁૉͷΛΧϯτ͢Δ߹ɺϥϜμࣜΛcount()ؔʹ͠·͢ɻ
4.4 ूؔ fold() ɾCollectionͷཁૉΛࠨ͔ΒӈॱʹΈࠐΈʢྦྷੵ͠ʣͳ͕Βૢ࡞Λߦ͏ͨΊʹ༻͞Ε·͢ɻ ɾॳظͱྦྷੵ݁ՌΛอ࣋͢ΔͨΊͷؔΛड͚औΓ·͢ɻ ɾfoldRight()foldͱٯॱʹཁૉΛ߹ܭ͍͖ͯ͠·͢ɻ
4.4 ूؔ reduce() ɾCollectionͷཁૉΛࠨ͔ΒӈॱʹΈࠐΈʢྦྷੵ͠ʣͳ͕Βૢ࡞Λߦ͏ͨΊʹ༻͞Ε·͢ɻ ɾfold()ͱͷҧ͍ॳظ͕ͳ͍͜ͱ
ߴͳؔͷ༻ྫ chunked windowed 05.
5.1 chunked Signature Ex. ɾCollectionΛࢦఆͨ͠αΠζͷνϟϯΫʢখ͞ͳ෦ʣʹׂ͠·͢ɻ
5.1 chunked σʔλϕʔεͷ όονૠೖ ɾେྔʹҰʹૠೖ͢ΔͱύϑΥʔϚϯε͕མͪͯ͠·͏߹ʹখ͚ʹ͢Δ
5.1 chunked େྔͷը૾ μϯϩʔυ ɾອըͷμϯϩʔυͳͲɺେྔͷը૾μϯϩʔυΛ͢Δࡍɺখ͚ʹ࣮ͯ͠ߦ͕؆୯ʹͰ͖Δ
5.2 windowed Signature Ex. ɾࢦఆͨ͠αΠζͷΟϯυʢεϥΠσΟϯά෦ʣΛϦετ͔Βੜ͠·͢
5.2 windowed Ҡಈฏۉઢ ɾ࣌ܥྻσʔλʹରͯ͠ҠಈฏۉΛܭࢉ͢Δ߹ɺwindowedؔඇৗʹศརͰ͢ɻ
5.2 windowed ύλʔϯݕग़ ɾςΩετྻͷதͰಛఆͷύλʔϯΛݕग़͢ΔࡍʹɺwindowedΛͬͯ࿈ ଓ͢Δ෦ΛνΣοΫͰ͖·͢ɻ
ࠩͷܭࢉ 5.2 windowed ɾྻͷྡ͢ΔཁૉؒͷࠩΛܭࢉ͢Δ߹ʹwindowedཱ͕ͪ·͢ɻ
Իͷղੳ 5.2 windowed
ύϑΥʔϚϯεͷ࠷దԽ sequence forจ 06.
6.1 Sequenceͱforจ ɾSequenceԆධՁΛߦ͏Collection typeͰେྔͷσʔλॲཧʹ͓͍ͯޮతͰ͢ ɾSequenceཁૉΛҰͭͣͭॲཧ͠ɺதؒૢ࡞Ԇ͞Εɺ࠷ऴૢ࡞͕ݺΕΔ·Ͱ࣮ߦ ͞Ε·ͤΜɻ͜ΕʹΑΓɺதؒૢ࡞ͷແବͳܭࢉΛආ͚Δ͜ͱ͕Ͱ͖·͢ɻ Sequence
ύϑΥʔϚϯεൺֱɹCollection, Sequence, for IntegerͷϦετʹରͯ͠Լهͷ࣮ߦΛߦ͍·͢ɻ IntegerͷϦετͷαΠζෳ࣮ࢪɻ ɾۮͷΈΛநग़ɻ ɾ͞Βʹɺͦͷத͔Β3ͰׂΓΕΔΛநग़ʢ6ͷഒʣɻ ɾͦΕΒͷΛೋɻ ɾ࠷ॳͷ5ݸͷཁૉΛऔΓग़͢ɻ 6.1
Sequenceͱforจ
Collection 6.1 Sequenceͱforจ
Sequence 6.1 Sequenceͱforจ
for 6.1 Sequenceͱforจ
ɾSequenceϦετͷαΠζ͕૿࣮͑ͯߦ΄΅มΘΒͳ͍͕ɺϦετ͕খͯ͘͞overhead͕ଘࡏͯ͠͠·͏ ɾCollectionϦετͷαΠζ͕૿͑Δͱൺྫͯ͠૿͑ΔɻϦετ͕େ͖͘ͳ͍࣌SequenceΑΓૣ͍ ɾforจ࠷ 6.1 Sequenceͱforจ Kotlin PlaygroundͰͷ10ճฏۉ
SequenceͱCollectionͷҧ͍ ɾCollection: ଈ࣌ධՁΛߦ͍ɺશͯͷཁૉΛϝϞϦʹอ࣋͠ɺॲཧ͠·͢ɻ ɾSequence: ԆධՁΛߦ͍ɺඞཁͳཁૉͷΈΛॲཧ͠·͢ɻ 6.1 Sequenceͱforจ
Sequenceͷૢ࡞ தؒૢ࡞: ऴૢ࡞: தؒૢ࡞ԆධՁ͞ΕΔૢ࡞Ͱ͢ɻྫ͑ɺmapɺfilterɺͳ Ͳؚ͕·Ε·͢ɻ͜ΕΒͷૢ࡞৽͍͠γʔέϯεΛฦ͠ɺ࣮ࡍ ͷॲཧऴૢ࡞͕ݺͼग़͞ΕΔ·ͰߦΘΕ·ͤΜɻ ऴૢ࡞SequenceΛফඅͯ݁͠ՌΛੜ͢Δૢ࡞Ͱ͢ɻྫ͑ ɺtoListɺsumɺcountɺfirstͳͲ͕͋Γ·͢ɻऴૢ࡞͕ݺ ΕΔͱɺશͯͷதؒૢ࡞͕࣮ߦ͞Ε·͢ɻ 6.1
Sequenceͱforจ
Collection 6.1 Sequenceͱforจ
Collection 6.1 Sequenceͱforจ
Sequence 6.1 Sequenceͱforจ
Sequence 6.1 Sequenceͱforจ
Sequence 6.1 Sequenceͱforจ ऴͷiterateͰCollectionͷཁૉ͝ͱʹॲཧΛ͍ͯ͠·͢ɻ
Sequenceͷ͍ॴ 1. ͔ͳΓେنͳίϨΫγϣϯʹରͯ͠ෳͷॲཧΛߦ͏߹ 2. ଟஈνΣʔϯΛల։͠ɺதؒϦετͷੜΛආ͚͍ͨ߹ 3. ύϑΥʔϚϯεͱϝϞϦ༻ྔΛ࠷దԽ͍͕ͨ͠ɺforจΛ͍ͨ͘ͳ͍߹ 6.1 Sequenceͱforจ
6.2 ·ͱΊ ɾCollectionؔ༏लͳͷͰɺجຊతʹCollectionؔΛ͍·͠ΐ͏ɻ ɾϦετͷαΠζ͕େ͖͘ύϑΥʔϚϯεͷϘτϧωοΫ͕͋ΓɺՄಡੑΛ ҡ͍࣋ͨ͠߹ʹSequenceΛ༻ ɾ࠷ΛٻΊΔͳΒforจ
Special Thanks KotlinެࣜϦϑΝϨϯε https://kotlinlang.org/docs/collections-overview.html Effective Kotlin: Prefer Sequence for big
collections with more than one processing step https://blog.kotlin-academy.com/effective-kotlin-use-sequence-for-bigger-collections-with-more-than-one- processing-step-649a15bb4bf
End Of doc.