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
LINE Digital Frontier - TECH
June 22, 2024
Technology
0
290
Kotlin Collection関数をマスター
「Kotlin Fest2024」の登壇資料です。
https://www.kotlinfest.dev/kotlin-fest-2024
LINE Digital Frontier - TECH
June 22, 2024
Tweet
Share
More Decks by LINE Digital Frontier - TECH
See All by LINE Digital Frontier - TECH
会社紹介資料
ldf_tech
1
490
SwiftSyntaxでUIKitとSwiftUIの使用率を完璧に計測できちゃう件について
ldf_tech
0
220
Kotlin 2.0が与えるAndroid開発の進化
ldf_tech
0
140
Road to Kotlin 〜10年続くPerl運用からの脱却〜
ldf_tech
0
20
Kotlin sealed classを用いた、 ユーザーターゲティングDSL(専用言語)と 実環境で秒間1,000万評価を行う処理系の事例紹介
ldf_tech
0
110
マンガアプリのメモリ改善と解析方法
ldf_tech
0
26
Other Decks in Technology
See All in Technology
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
520
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
180
UI State設計とテスト方針
rmakiyama
2
420
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
160
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
どちらを使う?GitHub or Azure DevOps Ver. 24H2
kkamegawa
0
680
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
370
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
オプトインカメラ:UWB測位を応用したオプトイン型のカメラ計測
matthewlujp
0
170
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
A better future with KSS
kneath
238
17k
Building an army of robots
kneath
302
44k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Adopting Sorbet at Scale
ufuk
73
9.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Mobile First: as difficult as doing things right
swwweet
222
9k
Rails Girls Zürich Keynote
gr2m
94
13k
Being A Developer After 40
akosma
87
590k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Scaling GitHub
holman
458
140k
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.