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
5.1k
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.8k
Jetpack Macrobenchmarkを試してみた
masayukisuda
1
280
MediaCodecで動画編集をしてみよう
masayukisuda
3
4.5k
Androidで動画編集アプリの技術選択
masayukisuda
1
1.3k
Other Decks in Programming
See All in Programming
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
240
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
220
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
870
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
180
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
950
Findy Team+ Awardを受賞したかった!ベストプラクティス応募内容をふりかえり、開発生産性向上もふりかえる / Findy Team Plus Award BestPractice and DPE Retrospective 2024
honyanya
0
140
asdf-ecspresso作って 友達が増えた話 / Fujiwara Tech Conference 2025
koluku
0
1.4k
php-conference-japan-2024
tasuku43
0
430
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
390
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.1k
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
7.7k
Beyond ORM
77web
11
1.6k
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
3
180
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Typedesign – Prime Four
hannesfritz
40
2.5k
Become a Pro
speakerdeck
PRO
26
5.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
210
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
19
2.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
960
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
192
16k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Mobile First: as difficult as doing things right
swwweet
222
9k
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.