Slide 1

Slide 1 text

Kotlin Collectionؔ਺ΛϚελʔ 2024.06.22 Masayuki Suda © LINE Digital Frontier Corporation ύϑΥʔϚϯεͱՄಡੑΛཱ྆

Slide 2

Slide 2 text

໨࣍ 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 ू໿ؔ਺

Slide 3

Slide 3 text

ΠϯτϩμΫγϣϯ 01. ࣗݾ঺հ ͖͔͚ͬ

Slide 4

Slide 4 text

1.1 ࣗݾ঺հ LINE Digital Frontier גࣜձࣾͱ͸

Slide 5

Slide 5 text

1.2 ͖͔͚ͬ ɾKotlin Collectionͷ֦ுؔ਺ͬͯ΋ͷ͍͢͝਺͕͋Δ͚Ͳ͋·Γ஌Βͳ͍ ɾશ෦͓͞Β͍͢Δػձ͕͋Δͱخ͍͠ͳ

Slide 6

Slide 6 text

Kotlin Collectionͷ֓ཁ ϑϨʔϜϫʔΫͷ֓ཁ ओཁͳΠϯλʔϑΣʔε 02.

Slide 7

Slide 7 text

2.1 ϑϨʔϜϫʔΫجຊߏ଄ ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638

Slide 8

Slide 8 text

2.1 ϑϨʔϜϫʔΫجຊߏ଄ ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638

Slide 9

Slide 9 text

2.1 ϑϨʔϜϫʔΫجຊߏ଄ Iterable Iterableͱ͸ɺίϨΫγϣϯ಺ͷཁૉΛॱ൪ʹऔಘͰ͖ΔΑ͏ʹ͢ΔͨΊͷجຊతͳΠϯ λʔϑΣʔε

Slide 10

Slide 10 text

hasNext(): ίϨΫγϣϯ಺ʹ࣍ͷཁૉ͕ଘࡏ͢Δ৔߹͸trueΛฦ͠·͢ɻ next(): ίϨΫγϣϯ಺ͷ࣍ͷཁૉΛฦ͠·͢ɻ Iterator 2.1 ϑϨʔϜϫʔΫجຊߏ଄

Slide 11

Slide 11 text

2.1 ϑϨʔϜϫʔΫجຊߏ଄ MutableIterator remove(): ݱࡏͷཁૉΛίϨΫγϣϯ͔Β࡟আ͠·͢

Slide 12

Slide 12 text

2.2 ओཁͳΠϯλʔϑΣʔε ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638

Slide 13

Slide 13 text

2.2 ओཁͳΠϯλʔϑΣʔε List ॱং෇͚͞ΕͨཁૉͷίϨΫγϣϯͰɺॏෳཁૉΛؚΉ͜ͱ͕Մೳ ΠϯσοΫεΛ࢖༻ͯ͠ΞΫηεͰ͖Δ

Slide 14

Slide 14 text

List

Slide 15

Slide 15 text

2.2 ओཁͳΠϯλʔϑΣʔε Set ॱং෇͚͞Εͳ͍ཁૉͷίϨΫγϣϯͰɺॏෳཁૉΛؚ·ͳ͍

Slide 16

Slide 16 text

2.2 ओཁͳΠϯλʔϑΣʔε Set

Slide 17

Slide 17 text

2.2 ओཁͳΠϯλʔϑΣʔε Map ɾΩʔͱ஋ͷϖΞͷίϨΫγϣϯͰɺ֤Ωʔ͸Ұҙ ɾList΍SetͱҧͬͯɺCollectionΠϯλʔϑΣΠεΛ௚઀ʹ͸ܧঝͤͣʹɺಠࣗͰMapΠϯλʔϑΣ ΠεΛ࣋ͭ

Slide 18

Slide 18 text

2.2 ओཁͳΠϯλʔϑΣʔε Map

Slide 19

Slide 19 text

2.2 ओཁͳΠϯλʔϑΣʔε ग़యɹhttps://medium.com/@imdjay/kotlin-collections-creating-list-set-and-maps-eb3d33500638

Slide 20

Slide 20 text

2.2 ओཁͳΠϯλʔϑΣʔε Immutable Mutable ɾKotlinͷίϨΫγϣϯ͸ɺimmutableͱmutableͷόʔδϣϯ͕͋Γ·͢ ɾimmutable͸มߋෆՄɺmutable͸มߋՄೳ ɾmutableʹ͸immutableʹ͸ଘࡏ͠ͳ͍add, removeͱ͍ͬͨมߋͰ͖Δؔ਺͕༻ҙ͞Ε͍ͯ·͢

Slide 21

Slide 21 text

ؔ਺ͷ঺հ1 ม׵ɺϑΟϧλϦϯά ཁૉͷ௥Ճɾ࡟আɺάϧʔϓԽ 03.

Slide 22

Slide 22 text

Collectionͷجຊૢ࡞Λߦ͏ϝϯόʔؔ਺Λఏڙ͠·͢ɻྫ͑͹ɺCollectionͷ isEmpty()΍Listͷget()͕͋Γ·͢ɻಠࣗͷίϨΫγϣϯΛ࣮૷͢Δࡍʹ͸ɺ͜Ε Βͷϝϯόʔؔ਺΋࣮૷͕ඞཁͰ͢ɻඪ४ϥΠϒϥϦʹ͸ɺ৽͍͠ίϨΫγϣϯ Λ؆୯ʹ࡞੒͢ΔͨΊͷࠎ࣮֨૷΋༻ҙ͞Ε͍ͯ·͢ɻ CollectionͷϑΟϧλϦϯάɺม׵ɺฒ΂ସ͑ͳͲΛߦ͏֦ுؔ਺΋ؚ·Ε͍ͯ· ͢ɻ֦ுؔ਺Λ࢖͏͜ͱͰɺطଘͷίϨΫγϣϯʹ৽͍͠ػೳΛॊೈʹ௥ՃͰ͖ ·͢ɻ ϝϯόʔؔ਺ ֦ுؔ਺

Slide 23

Slide 23 text

3.1 ม׵ طଘͷCollection͔Β৽͍͠CollectionΛߏங͠·͢ طଘͷίϨΫγϣϯ͔Β৽͍͠ίϨΫγϣϯΛߏங͠·͢ Map ผͷCollectionͷཁૉʹର͢Δؔ਺ͷ݁Ռ͔ΒCollectionΛ࡞੒ ͠·͢ Zip ྆ํͷCollection಺ͷಉ͡Ґஔʹ͋Δཁૉ͔ΒϖΞΛߏங͠· ͢ Associate CollectionཁૉͱͦΕΒʹؔ࿈෇͚ΒΕͨಛఆͷ஋͔ΒϚοϓ ΛߏஙͰ͖·͢ Flatten ωετ͞ΕͨCollectionΛҰͭͷCollectionʹ͠·͢

Slide 24

Slide 24 text

3.1 ม׵ Map ɾCollectionͷ֤ཁૉʹࢦఆͨ͠ม׵ؔ਺Λద༻͠ɺͦͷ݁Ռ͔Β৽͍͠CollectionΛ࡞੒͠·͢ɻ ɾجຊతͳϚοϐϯάؔ਺͸map()Ͱ͢ɻ ɾࢦఆ͞ΕͨϥϜμؔ਺Λ֤ཁૉʹద༻͠ɺϥϜμ݁ՌͷϦετΛฦ͠·͢ɻ ɾ݁Ռͷॱং͸ݩͷཁૉͷॱংͱಉ͡Ͱ͢ɻ map() Signature Ex.

Slide 25

Slide 25 text

3.1 ม׵ Map ɾཁૉΠϯσοΫεΛҾ਺ͱͯ͠࢖༻͢Δม׵ʹ͸mapIndexed()Λ࢖༻͠·͢ɻ mapIndexed() Signature Ex.

Slide 26

Slide 26 text

3.1 ม׵ ɾม׵ʹΑΓಛఆͷཁૉͰnull͕ੜ੒͞ΕΔ৔߹ ɾmapNotNull()Λ࢖༻͢Δ͜ͱͰɺ݁ՌͷCollection͔ΒnullΛআ֎Ͱ͖·͢ɻ Map mapNotNull() Signature Ex.

Slide 27

Slide 27 text

3.1 ม׵ ɾཁૉΠϯσοΫεΛҾ਺ͱͯ͠࢖༻͢Δࡍɺม׵ʹΑΓಛఆͷཁૉͰnull͕ੜ੒͞ΕΔ৔߹ ɾmapIndexedNotNull()Λ࢖༻͢Δ͜ͱͰɺ݁ՌͷCollection͔ΒnullΛআ֎Ͱ͖·͢ɻ Map mapIndexedNotNull() Signature Ex.

Slide 28

Slide 28 text

3.1 ม׵ ɾCollectionͷ֤ཁૉʹରͯ͠ࢦఆ͞Εͨม׵ؔ਺Λద༻͠ɺ݁ՌΛࢦఆ͞ΕͨѼઌCollectionʹ௥ Ճ͢Δ ɾ৽͍͠CollectionΛੜ੒͢ΔͷͰ͸ͳ͘ɺطଘͷCollectionʹม׵݁ՌΛ௥Ճ͠·͢ɻ ɾmapIndexedTo(), mapNotNullTo()ͳͲɺଞͷmapܥؔ਺શͯʹto͕͍͍ͭͯ·͢ɻ Map mapTo() Signature Ex.

Slide 29

Slide 29 text

3.1 ม׵ Zip ɾZipม׵͸ɺೋͭͷCollection಺ͷಉ͡Ґஔʹ͋Δཁૉ͔ΒϖΞΛߏங͠·͢ɻ ɾ݁ՌͷϖΞͷ࠷ॳͷཁૉ͸ɺReceiver CollectionͷཁૉͰ͢ɻ ɾCollectionͷαΠζ͕ҟͳΔ৔߹ɺ݁Ռ͸খ͍͞ํͷαΠζʹͳΓ·͢ɻ ɾzip()͸ɺa zip bܗࣜͷதஔͰ΋ݺͼग़ͤ·͢ɻ ɾܭࢉྔ͸O(min(n, m)) Signature zip() Ex.

Slide 30

Slide 30 text

3.1 ม׵ ɾzip()͸2ͭͷύϥϝʔλʢड৴ଆཁૉͱҾ਺ཁૉʣΛऔΔม׵ؔ਺Λ࢖༻ͯ͠ݺͼग़͢͜ͱ΋Ͱ͖·͢ɻ ɾ͜ͷ৔߹ɺ݁ՌϦετʹ͸ɺड৴ଆཁૉͱҾ਺ཁૉͷϖΞʹରͯ͠ม׵ؔ਺Λద༻ͨ͠໭Γ஋ؚ͕·Ε·͢ɻ Zip zip() Ex. Signature

Slide 31

Slide 31 text

3.1 ม׵ ɾϖΞͷϦετ͕͋Δ৔߹͸ɺ͜ΕΒͷϖΞ͔Β 2 ͭͷϦετΛߏங͢Δٯม׵ (ղౚ) Λ࣮ߦͰ͖·͢ɻ Zip unzip() Signature Ex.

Slide 32

Slide 32 text

3.1 ม׵ Associate ɾCollectionΛMapʹม׵͢ΔͨΊͷศརͳؔ਺Ͱ͢ associateWith() Signature Ex.

Slide 33

Slide 33 text

3.1 ม׵ Associate ɾassociateByؔ਺Ͱ͸ɺCollectionͷ֤ཁૉ͕݁ՌͷϚοϓ಺ͰͲͷΑ͏ʹΩʔͱ஋ͷϖΞʹม׵͞ΕΔ΂͖ ͔Λࢦఆ͢Δ͜ͱ͕Ͱ͖·͢ associateBy() Signature Ex.

Slide 34

Slide 34 text

3.1 ม׵ Associate ɾ৽͍͠CollectionΛੜ੒͢ΔͷͰ͸ͳ͘ɺطଘͷMapʹཁૉΛ௥Ճ͠·͢ɻ ɾassociateByTo(), associateWithTo()ͳͲɺଞͷassociateܥؔ਺શͯʹto͕͍͍ͭͯ·͢ɻ associateTo() Signature Ex.

Slide 35

Slide 35 text

3.1 ม׵ Associateɹ ஫ҙ఺ ɾPair ObjectΛଟ਺ੜ੒ͯ͠͠·͏͜ͱͷݒ೦ ɹɾ֤Collectionཁૉʹରͯ͠୹໋ͷPair ObjectΛੜ੒͠·͢ɻ͜ΕʹΑΓɺҰ࣌తͳϝϞϦ࢖༻ྔ͕૿Ճ͠·͢ɻ ɹɾେن໛ͳCollectionʹରͯ͠associate()Λ࢖༻͢Δͱɺଟ਺ͷPair Object͕࡞੒͞ΕɺΨϕʔδίϨΫγϣϯͷ ෛ୲͕૿Ճ͢ΔՄೳੑ͕͋Γ·͢ɻ ɹɾPair ObjectΛੜ੒͢Δίετ͕͔͔Γ·͢ɻ֤ཁૉʹରͯ͠ϖΞΛ࡞੒͢ΔͨΊɺଟ਺ͷObjectੜ੒͕ൃੜ͠ ·͢ɻ ɹɾObjectੜ੒ͷΦʔόʔϔου͕ྦྷੵ͢ΔͱɺύϑΥʔϚϯεʹӨڹΛ༩͑Δ͜ͱ͕͋Γ·͢ɻ ɾύϑΥʔϚϯε͕ॏཁͰͳ͍৔߹΍ɺΩʔͱ஋ΛҰॹʹੜ੒͢Δඞཁ͕͋Δ৔߹ʹద͍ͯ͠·͢ɻ

Slide 36

Slide 36 text

3.1 ม׵ Flatten ɾωετ͞ΕͨCollectionͷ͢΂ͯͷཁૉͷ୯ҰͷϦετΛฦ͠·͢ɻ Signature Ex. flatten()

Slide 37

Slide 37 text

Flatten 3.1 ม׵ Signature Ex. flatMap() ɾCollectionͷ֤ཁૉʹରͯ͠ࢦఆ͞Εͨม׵ؔ਺Λద༻͠ɺͦͷ݁Ռͱͯ͠ಘΒΕͨCollectionΛ Ұͭʹ·ͱΊͯฏୱԽ͠·͢ɻ ɾܭࢉྔ͸O(n + m) ɹɾn͸ݩͷίϨΫγϣϯͷཁૉ਺ɻ ɹɾm͸ม׵͞ΕͨίϨΫγϣϯͷશཁૉ਺ͷ߹ܭɻ ɾΦʔόʔϔου ɹɾ֤ཁૉʹରͯ͠ϦετΛੜ੒͢ΔͨΊɺෆཁͳΦʔόʔϔου͕ൃੜ͢ΔՄೳੑ͕͋Γ·͢ɻ

Slide 38

Slide 38 text

3.2 ϑΟϧλϦϯά طଘͷCollectionΛϑΟϧλʔ͠ɺ৽͍͠CollectionΛߏங͠·͢ filter(), filterNot() جຊతͳϑΟϧλϦϯάɻ৚݅ʹ߹க͢Δ͔ɺ͠ͳ͍͔ͰϑΟϧλϦ ϯά͠·͢ɻ filterIndexed() Indexͱ஋ʹج͍ͮͯཁૉΛϑΟϧλϦϯά͠·͢ɻ filterIsInstance(), filterNotNull() ಛఆͷܕͷཁૉ ɺnull஋Λߟྀͯ͠ϑΟϧλϦϯά͠·͢ɻ partition() ৚݅ʹ߹க͢Δཁૉͱ͠ͳ͍ཁૉΛ2ͭͷϦετʹ෼͚·͢ɻ any(), none(), all() Collection಺ͷཁૉ͕৚݅ʹ߹க͢Δ͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ filterTo() طଘͷCollectionʹϑΟϧλϦϯά݁ՌΛ௥Ճ͠·͢ɻ

Slide 39

Slide 39 text

3.2 ϑΟϧλϦϯά filter() Signature Ex. ɾ৚݅ʹ߹க͢ΔཁૉΛϦετͰฦ͠·͢ɻ

Slide 40

Slide 40 text

3.2 ϑΟϧλϦϯά filterNot() Signature Ex. ɾ৚݅ʹ߹க͠ͳ͍ཁૉΛϦετͰฦ͠·͢ɻ

Slide 41

Slide 41 text

3.2 ϑΟϧλϦϯά filterIndexed() Signature Ex. ɾΠϯσοΫεͱ஋ʹج͍ͮͯཁૉΛϑΟϧλϦϯά͠·͢ɻ

Slide 42

Slide 42 text

3.2 ϑΟϧλϦϯά filterIsInstance() Signature Ex. ɾಛఆͷܕͷཁૉΛϑΟϧλϦϯά͠·͢ɻ

Slide 43

Slide 43 text

3.2 ϑΟϧλϦϯά filterNotNull() Signature Ex. ɾnullҎ֎ͷཁૉΛϑΟϧλϦϯά͠·͢ɻ

Slide 44

Slide 44 text

3.2 ϑΟϧλϦϯά filterTo() Signature Ex. ɾطଘͷCollectionʹϑΟϧλϦϯά݁ՌΛ௥Ճ͠·͢ɻ ɾfilterNotTo(), filterNotNullTo(), filterIsInstanceTo(), filterIndexedTo(),΋ଘࡏ͢Δ

Slide 45

Slide 45 text

partition() Signature Ex. 3.2 ϑΟϧλϦϯά ɾ৚݅ʹ߹க͢Δཁૉͱ͠ͳ͍ཁૉΛ2ͭͷϦετʹ෼͚·͢ɻ

Slide 46

Slide 46 text

3.2 ϑΟϧλϦϯά any() Signature Ex. ɾCollection಺ͷཁૉ͕৚݅ʹ߹க͢Δ΋ͷ͕͋Δ͔ΛνΣοΫ͠·͢ɻ

Slide 47

Slide 47 text

3.2 ϑΟϧλϦϯά all() Signature Ex. ɾCollection಺ͷཁૉ͕৚݅ʹશͯ߹க͢Δ͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ

Slide 48

Slide 48 text

3.2 ϑΟϧλϦϯά none() Signature Ex. ɾCollection಺ͷཁૉ͕৚݅ʹશͯ߹க͍ͯ͠ͳ͍͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ

Slide 49

Slide 49 text

3.3 ཁૉͷ௥Ճɾ࡟আ Collectionʹରͯ͠+ʢplusʣͱ-ʢminusʣԋࢉࢠ͕ఆٛ͞Ε͍ͯ·͢ɻ ͜ΕΒ͸৽͍͠ಡΈऔΓઐ༻ͷCollectionΛฦ͠·͢ɻ Ex. ※ ৽͘͠listΛੜ੒͢Δ෼ɺϝϞϦΛফඅ͠·͢ɻϝϞϦ͕ݫ͍͠৔߹͸ mutableListΛ࢖͏ݕ౼ͳͲ͠·͠ΐ͏ɻ

Slide 50

Slide 50 text

3.4 άϧʔϓԽ groupBy() ࢦఆͨ͠Ωʔબ୒ؔ਺ʹΑͬͯཁૉΛάϧʔϓԽ͠ɺϚοϓͱͯ͠ฦ ͠·͢ɻ groupingBy() άϧʔϓԽΛ஗Ԇ࣮ߦ͢ΔGroupingΠϯελϯεΛฦ͠ɺ eachCount()ɺfold()ɺreduce(), aggregate()ͳͲͷૢ࡞Λαϙʔτ͠ ·͢ɻ

Slide 51

Slide 51 text

3.4 άϧʔϓԽ Signature Ex. groupBy() ɾطଘͷCollectionΛࢦఆ͞ΕͨΩʔʹج͍ͮͯάϧʔϓԽ͠ɺMapͱͯ͠ฦ͠·͢ɻ

Slide 52

Slide 52 text

3.4 άϧʔϓԽ Signature Ex. groupBy() ɾطଘͷCollectionΛࢦఆ͞ΕͨΩʔʹج͍ͮͯάϧʔϓԽ͠ɺvalueTransformͰݩͷཁૉΛมߋ͠Mapͱͯ͠ฦ͠ ·͢ɻ

Slide 53

Slide 53 text

3.4 άϧʔϓԽ groupingBy() Signature ɾ஗ԆධՁΛߦ͍ɺதͰGroupingΦϒδΣΫτΛੜ੒͍ͯ͠·͢ɻऴ୺ॲཧͰMapΛฦ͠·͢ɻ

Slide 54

Slide 54 text

3.4 άϧʔϓԽ groupingBy() Signature ɾgroupingByΛ࣮ߦ͚ͨͩ͠Ͱ͸άϧʔϓԽ͸ى͜Γ·ͤΜɻeachCount()ɺfold()ɺreduce(), aggregate()ͳͲͷऴ୺ؔ਺Λ࣮ߦͯ͠ॳΊͯgroupingԽ͞Ε·͢ɻ Ex.

Slide 55

Slide 55 text

3.4 άϧʔϓԽ groupingBy{}.eachCount() Signature Ex. ɾ֤άϧʔϓͷཁૉ਺ΛΧ΢ϯτ͢ΔϝιουͰ͢ɻ ɾgroupingByͰάϧʔϓԽͨ͠ޙʹeachCountΛݺͼग़͢ͱɺ֤άϧʔϓͷΩʔͱͦͷཁૉ਺ΛϚοϓ ͱͯ͠ฦ͠·͢

Slide 56

Slide 56 text

groupingBy{}.reduce() 3.4 άϧʔϓԽ Signature Ex. ɾάϧʔϓ͝ͱͷཁૉΛྦྷੵ͠ɺ୯Ұͷ஋ʹू໿͢ΔͨΊʹ࢖༻͞Ε·͢ɻ ɾ֤άϧʔϓͷ࠷ॳͷཁૉΛॳظ஋ͱ͠ɺࢦఆͨ͠ૢ࡞Λద༻͠·͢ɻ

Slide 57

Slide 57 text

groupingBy{}.fold() 3.4 άϧʔϓԽ Signature Ex. ɾ֤άϧʔϓʹରͯ͠ྦྷੵૢ࡞Λߦ͏ͨΊʹ࢖༻͞Ε·͢ɻ ɾॳظ஋ͱྦྷੵؔ਺Λࢦఆͯ͠ɺ֤άϧʔϓͷཁૉΛॱ࣍ॲཧ͠ɺྦྷੵ݁ՌΛੜ੒͠·͢ɻ

Slide 58

Slide 58 text

groupingBy{}.aggregate() 3.4 άϧʔϓԽ Signature Ex. ɾάϧʔϓ͝ͱʹΧελϜूܭૢ࡞Λߦ͏ͨΊͷؔ਺Ͱ͢ɻ ɾreduce΍foldͰ࣮ݱ͕೉͍͠৔߹ͷΧελϜૢ࡞ͱͯ͠࢖͑·͢ɻ

Slide 59

Slide 59 text

groupingBy{} .aggregate() 3.4 άϧʔϓԽ

Slide 60

Slide 60 text

3.4 άϧʔϓԽ groupByTo() ɾطଘͷMapʹάϧʔϓԽ͞ΕͨཁૉΛ௥Ճ͠·͢ɻ ɾgroupingByͷeachCount, fold, reduce, aggregateʹ΋Toϝιου͸ଘࡏ͢Δ Signature Ex.

Slide 61

Slide 61 text

4.1 ෦෼औಘ slice() ࢦఆͨ͠ΠϯσοΫεͷϦετΛجʹ৽͍͠ϦετΛ࡞੒͠·͢ɻ take() Ϧετͷઌ಄͔Βࢦఆͨ͠਺ͷཁૉΛऔಘ͠·͢ɻ drop() Ϧετͷઌ಄͔Βࢦఆͨ͠਺ͷཁૉΛআ֎ͨ͠࢒ΓΛऔಘ͠·͢ɻ chunked() ϦετΛࢦఆͨ͠αΠζͷνϟϯΫʢখ͞ͳ෦෼ʣʹ෼ׂ͠·͢ɻ windowed() ࢦఆͨ͠αΠζͷ΢Οϯυ΢ʢεϥΠσΟϯά෦෼ʣΛϦετ͔Β ੜ੒͠·͢ɻ طଘͷCollection͔ΒҰ෦ΛऔΓग़͠ɺsublistΛੜ੒͠·͢ɻ

Slide 62

Slide 62 text

4.1 ෦෼औಘ slice() Signature Ex. ɾCollection಺ͷཁૉ͕৚݅ʹશͯ߹க͢Δ͔Ͳ͏͔ΛνΣοΫ͠·͢ɻ ɾܭࢉྔ͸O(n) ɾࢦఆͨ͠ΠϯσοΫεͷϦετΛجʹ৽͍͠ϦετΛ࡞੒͠·͢ɻ ɾҾ਺͸IntRangeͱIterableͷ̎ύλʔϯ͋Γ·͢ɻ

Slide 63

Slide 63 text

4.1 ෦෼औಘ take() takeLast() Signature Ex. ɾϦετͷઌ಄͔Βࢦఆͨ͠਺ͷཁૉΛऔಘ͠·͢ɻ ɾtakeLast͸Ϧετͷ຤ඌ͔Βࢦఆͨ͠਺ͷཁૉΛऔಘ͠·͢ɻ

Slide 64

Slide 64 text

4.1 ෦෼औಘ takeWhile() takeLastWhile() Signature Ex. ɾࢦఆ͞Εͨ৚͕݅ਅͰ͋ΔݶΓɺCollectionͷཁૉΛઌ಄͔Βॱʹऔಘ͠·͢ɻ ɾtakeLast͸ࢦఆ͞Εͨ৚͕݅ਅͰ͋ΔݶΓɺCollectionͷཁૉΛ຤ඌ͔Βॱʹऔಘ͠·͢ɻ

Slide 65

Slide 65 text

4.1 ෦෼औಘ Signature Ex. drop() dropLast() ɾϦετͷઌ಄͔Βࢦఆͨ͠਺ͷཁૉΛআ֎ͨ͠࢒ΓΛऔಘ͠·͢ɻ ɾdropLast͸Ϧετͷ຤ඌ͔Βࢦఆͨ͠਺ͷཁૉΛআ֎ͨ͠࢒ΓΛऔಘ͠·͢ɻ

Slide 66

Slide 66 text

4.1 ෦෼औಘ dropWhile() dropLastWhile() Signature Ex. ɾࢦఆ͞Εͨ৚͕݅ਅͰ͋ΔݶΓɺCollectionͷཁૉΛઌ಄͔Βॱʹআ֎͠·͢ɻ ɾdropLast͸ࢦఆ͞Εͨ৚͕݅ਅͰ͋ΔݶΓɺCollectionͷ຤ඌ͔ΒॱʹཁૉΛআ֎͠·͢ɻ

Slide 67

Slide 67 text

chunked() 4.1 ෦෼औಘ Signature Ex. ɾCollectionΛࢦఆͨ͠αΠζͷνϟϯΫʢখ͞ͳ෦෼ʣʹ෼ׂ͠·͢ɻ ɾνϟϯΫͨ͠ޙʹɺtransform͢Δࣄ΋ՄೳͰ͢ ɾେྔͷσʔλΛҰ౓ʹॲཧ͢ΔͷͰ͸ͳ͘ɺগͣͭ͠ޮ཰తʹॲཧ͍ͨ࣌͠ʹ˕

Slide 68

Slide 68 text

4.1 ෦෼औಘ windowed() Signature Ex. ɾࢦఆͨ͠αΠζͷ΢Οϯυ΢ʢεϥΠσΟϯά෦෼ʣΛϦετ͔Βੜ੒͠·͢ ɾ෼ׂͨ͠ޙʹɺtransform͢Δࣄ΋ՄೳͰ͢ ɾܭࢉྔ: O(n * k) ʢn͸ݩͷϦετͷαΠζɺk͸΢Οϯυ΢ͷαΠζʣ

Slide 69

Slide 69 text

4.1 ෦෼औಘ windowed() ɾؔ਺ͷ໊લɺwindowedͷ֓೦͸ɺ࿈ଓత·ͨ͸௕͍σʔλγʔέϯεΛখ͞ͳʮ΢Οϯυ΢ʯʹ෼ׂɻ ɾͦΕͧΕͷ΢Οϯυ΢ʢࠓ૭͔Βݟ͍͑ͯΔ෦෼ʣʹରͯ͠ݸผʹॲཧΛߦ͏ͱ͍͏ํ๏࿦ʹج͍͍ͮͯ·͢ɻ ग़యɹhttps://www.baeldung.com/kotlin/collections-api

Slide 70

Slide 70 text

4.2 ୯Ұཁૉऔಘ first() Collectionͷ࠷ॳͷཁૉΛऔಘ last() Collectionͷ࠷ޙͷཁૉΛऔಘ elementAt() ࢦఆ͞ΕͨindexͷཁૉΛऔಘ

Slide 71

Slide 71 text

4.1 ෦෼औಘ Signature Ex. first() ɾCollectionͷ࠷ॳͷཁૉΛऔಘ͠·͢ɻ৚݅Λࢦఆ͢Δ͜ͱ΋ՄೳͰ͢ɻ ɾແ͚Ε͹NoSuchElementException͕throw͞ΕΔͷͰ஫ҙ

Slide 72

Slide 72 text

4.1 ෦෼औಘ Signature Ex. last() ɾCollectionͷ࠷ޙͷཁૉΛऔಘ͠·͢ɻ৚݅Λࢦఆ͢Δ͜ͱ΋ՄೳͰ͢ɻ ɾແ͚Ε͹NoSuchElementException͕throw͞ΕΔͷͰ஫ҙ

Slide 73

Slide 73 text

4.1 ෦෼औಘ Signature Ex. firstOrNull() ɾCollectionͷ࠷ॳͷཁૉΛऔಘ͠·͢ɻ৚݅Λࢦఆ͢Δ͜ͱ΋ՄೳͰ͢ɻ ɾແ͚Ε͹null͕ฦ͖ͬͯ·͢ɻ

Slide 74

Slide 74 text

å 4.1 ෦෼औಘ Signature Ex. lastOrNull() ɾCollectionͷ࠷ޙͷཁૉΛऔಘ͠·͢ɻ৚݅Λࢦఆ͢Δ͜ͱ΋ՄೳͰ͢ɻ ɾແ͚Ε͹null͕ฦ͖ͬͯ·͢ɻ

Slide 75

Slide 75 text

4.1 ෦෼औಘ Signature Ex. firstNotNullOf() ɾCollection಺ͷཁૉΛࢦఆ͞Εͨม׵ؔ਺Λద༻͠ɺ࠷ॳͷඇnullͷ݁ՌΛฦ͠·͢ɻ ɾແ͚Ε͹NoSuchElementException͕throw͞ΕΔͷͰ஫ҙ

Slide 76

Slide 76 text

4.1 ෦෼औಘ Signature Ex. firstNotNullOfNull() ɾCollection಺ͷཁૉΛࢦఆ͞Εͨม׵ؔ਺Λద༻͠ɺ࠷ॳͷඇnullͷ݁ՌΛฦ͠·͢ɻ ɾແ͚Ε͹null͕ฦ͖ͬͯ·͢

Slide 77

Slide 77 text

4.1 ෦෼औಘ elementAt() elementAtOrNull() elementAtOrElse() ɾࢦఆ͞ΕͨindexͷཁૉΛऔಘ͠·͢ɻ ɾelementAt()͸ࢦఆ͞Εͨindex͕listͷൣғ֎ͩͬͨ৔߹IndexOutOfBoundsException͕throw ɾelementAtOrNull()͸ࢦఆ͞Εͨindex͕listͷൣғ֎ͩͬͨ৔߹null ɾelementAtOrElse()͸ࢦఆ͞Εͨindex͕listͷൣғ֎ͩͬͨ৔߹ࢦఆͨ͠default஋

Slide 78

Slide 78 text

4.3 ฒͼସ͑ sorted() ࣗવॱংʹج͍ͮͯཁૉΛιʔτ͠·͢ sortedDescending() ࣗવॱংʹج͍ͮͯཁૉΛ߱ॱʹιʔτ͠·͢ sortedBy() ࢦఆͨ͠ηϨΫλʔؔ਺ʹج͍ͮͯཁૉΛιʔτ͠·͢ sortedByDescending() ࢦఆͨ͠ηϨΫλʔؔ਺ʹج͍ͮͯཁૉΛ߱ॱʹιʔτ͠ ·͢ sortedWith() ࢦఆͨ͠ίϯύϨʔλʹج͍ͮͯཁૉΛιʔτ͠·͢ reversed() ཁૉͷॱংΛ൓సͤ͞·͢ shuffled() ཁૉΛϥϯμϜʹฒ΂ସ͑·͢

Slide 79

Slide 79 text

sorted() sortedDescending() ɾsorted()͸ঢॱʹsort͠·͢ ɾsortedDescending()͸߱ॱʹsort͠·͢ 4.3 ฒͼସ͑

Slide 80

Slide 80 text

sortedBy() sortedByDescending() ɾsortedByؔ਺͸ɺࢦఆͨ͠ϓϩύςΟΛج४ʹCollectionͷཁૉΛঢॱʹsort͠·͢ɻ ɾsortedByDescendingؔ਺͸ɺࢦఆͨ͠ϓϩύςΟΛج४ʹίϨΫγϣϯͷཁૉΛ߱ॱʹsort͠·͢ɻ 4.3 ฒͼସ͑

Slide 81

Slide 81 text

sortedWith() ɾsortedWithؔ਺͸ɺࢦఆͨ͠ComparableΛ࢖ͬͯίϨΫγϣϯͷཁૉΛsort͠·͢ɻ ɾ͜ͷαϯϓϧͰ͸ɺcompareByͱthenByΛ࢖ͬͯComparableΛ࡞੒͠ɺsortedWithؔ਺ ʹ౉͍ͯ͠·͢ɻ·ͣɺ໊લΛج४ʹsort͠ɺ࣍ʹ೥ྸΛج४ʹsort͍ͯ͠·͢ɻ 4.3 ฒͼସ͑

Slide 82

Slide 82 text

reserved()
 asReserved() ɾreserved()΋asReserved()΋originalͷCollectionΛมߋͤͣʹٯॱʹฒ΂ସ͑ͨ஋Λฦ͠·͢ɻ ɾreserved()͸৽͍͠ϦετΛฦ͠ɺasReserved()͸ݩͷϦετΛࢀরͯ͠࡞੒͢ΔͨΊɺ৽͍͠Ϧετ ͸࡞੒͠ͳ͍ ɹɾCollectionͷن໛͕େ͖͍ͱasReserved()ͷํ͕ϝϞϦޮ཰ɺ࣮ߦ଎౓͕ૣ͍৔߹͕͋Γ·͢ɻ 4.3 ฒͼସ͑

Slide 83

Slide 83 text

asReserved() ɾasReserved͸ࢀরͷindexΛٯॱʹ͍ͯ͠Δ͚ͩͰ࣮ݱ͍ͯ͠·͢ɻ৽͍͠list͸࡞੒͠ͳ͍ɻ 4.3 ฒͼସ͑

Slide 84

Slide 84 text

shuffled() 4.3 ฒͼସ͑ ɾϦετͷཁૉΛϥϯμϜʹγϟοϑϧͯ͠৽͍͠ϦετΛ࡞੒͠·͢ɻݩͷϦετ͸มߋ͞Ε·ͤΜɻ ɾϥϯμϜγʔυΛࢦఆͯ͠γϟοϑϧΛߦ͏͜ͱͰɺ࠶ݱՄೳͳγϟοϑϧ݁ՌΛಘΔ͜ͱ΋Ͱ͖·͢ɻ

Slide 85

Slide 85 text

4.4 ू໿ؔ਺ maxOrNull() CollectionͷཁૉͷதͰ࠷େͷ΋ͷΛฦ͠·͢ minOrNull() CollectionͷཁૉͷதͰ࠷খͷ΋ͷΛฦ͠·͢ sum() CollectionͷཁૉΛ߹ܭ͢ΔͨΊʹ࢖༻͞Ε·͢ average() Collectionͷ਺஋ཁૉͷฏۉΛܭࢉ͢ΔͨΊʹ࢖༻͞Ε·͢ count() Collectionͷཁૉͷ਺Λ਺͑ΔͨΊʹ࢖༻͞Ε·͢ fold() ॳظ஋ͱྦྷੵૢ࡞Λࢦఆͯ͠ࠨ͔ΒӈʹίϨΫγϣϯΛંΓͨͨΉɻ reduce() ॳظ஋ͳ͠Ͱ࠷ॳͷཁૉΛॳظ஋ͱͯ͠ࠨ͔ΒӈʹίϨΫγϣϯΛॖখ͢ Δɻ

Slide 86

Slide 86 text

maxOrNull() ɾCollectionͷཁૉͷதͰ࠷େͷཁૉΛฦ͠·͢ɻ ɾCollectionͷཁૉ͕ແ͚Ε͹nullΛฦ͠·͢ɻ ɾmax()ؔ਺͸ඇਪ঑ͱͳͬͯ·͢ɻ 4.4 ू໿ؔ਺

Slide 87

Slide 87 text

maxByOrNull() ɾΧελϜΦϒδΣΫτͷϓϩύςΟΛج४ʹͯ͠࠷େ஋Λ؆୯ʹऔಘͰ͖·͢ɻ 4.4 ू໿ؔ਺

Slide 88

Slide 88 text

4.4 ू໿ؔ਺ minOrNull() ɾCollectionͷཁૉͷதͰ࠷খͷཁૉΛฦ͠·͢ɻ ɾCollection͕ແ͚Ε͹nullΛฦ͠·͢ɻ ɾmin()ؔ਺͸ඇਪ঑ͱͳͬͯ·͢ɻ

Slide 89

Slide 89 text

4.4 ू໿ؔ਺ minByOrNull() ɾΧελϜΦϒδΣΫτͷϓϩύςΟΛج४ʹͯ͠࠷খ஋Λ؆୯ʹऔಘͰ͖·͢ɻ

Slide 90

Slide 90 text

4.4 ू໿ؔ਺ sum() ɾCollectionͷཁૉͷ߹ܭΛฦ͠·͢ɻ

Slide 91

Slide 91 text

4.4 ू໿ؔ਺ sumOf() ɾΧελϜΦϒδΣΫτͷϓϩύςΟΛج४ʹͨ͠߹ܭ஋ͷऔಘ͕Ͱ͖·͢ɻ

Slide 92

Slide 92 text

4.4 ू໿ؔ਺ average() ɾCollection಺ͷ਺஋ͷฏۉ஋Λܭࢉ͢ΔͨΊʹ࢖༻͞Ε·͢ɻ ɾۭͷCollectionͷ৔߹ɺaverage()ؔ਺͸NaNʢNot a NumberʣΛฦ͠·͢ɻ

Slide 93

Slide 93 text

4.4 ू໿ؔ਺ count() ɾCollectionͷཁૉͷ਺Λऔಘ ɾಛఆͷ৚݅Λຬͨ͢ཁૉͷ਺ΛΧ΢ϯτ͢Δ৔߹͸ɺϥϜμࣜΛcount()ؔ਺ʹ౉͠·͢ɻ

Slide 94

Slide 94 text

4.4 ू໿ؔ਺ fold() ɾCollectionͷཁૉΛࠨ͔Βӈ΁ॱʹ৞ΈࠐΈʢྦྷੵ͠ʣͳ͕Βૢ࡞Λߦ͏ͨΊʹ࢖༻͞Ε·͢ɻ ɾॳظ஋ͱྦྷੵ݁ՌΛอ࣋͢ΔͨΊͷؔ਺Λड͚औΓ·͢ɻ ɾfoldRight()͸foldͱ͸ٯॱʹཁૉΛ߹ܭ͍͖ͯ͠·͢ɻ

Slide 95

Slide 95 text

4.4 ू໿ؔ਺ reduce() ɾCollectionͷཁૉΛࠨ͔Βӈ΁ॱʹ৞ΈࠐΈʢྦྷੵ͠ʣͳ͕Βૢ࡞Λߦ͏ͨΊʹ࢖༻͞Ε·͢ɻ ɾfold()ͱͷҧ͍͸ॳظ஋͕ͳ͍͜ͱ

Slide 96

Slide 96 text

ߴ౓ͳؔ਺ͷ࢖༻ྫ chunked windowed 05.

Slide 97

Slide 97 text

5.1 chunked Signature Ex. ɾCollectionΛࢦఆͨ͠αΠζͷνϟϯΫʢখ͞ͳ෦෼ʣʹ෼ׂ͠·͢ɻ

Slide 98

Slide 98 text

5.1 chunked σʔλϕʔε΁ͷ όονૠೖ ɾେྔʹҰ౓ʹૠೖ͢ΔͱύϑΥʔϚϯε͕མͪͯ͠·͏৔߹ʹখ෼͚ʹ͢Δ

Slide 99

Slide 99 text

5.1 chunked େྔͷը૾ μ΢ϯϩʔυ ɾອըͷμ΢ϯϩʔυͳͲɺେྔͷը૾μ΢ϯϩʔυΛ͢Δࡍɺখ෼͚ʹ࣮ͯ͠ߦ͕؆୯ʹͰ͖Δ

Slide 100

Slide 100 text

5.2 windowed Signature Ex. ɾࢦఆͨ͠αΠζͷ΢Οϯυ΢ʢεϥΠσΟϯά෦෼ʣΛϦετ͔Βੜ੒͠·͢

Slide 101

Slide 101 text

5.2 windowed Ҡಈฏۉઢ ɾ࣌ܥྻσʔλʹରͯ͠ҠಈฏۉΛܭࢉ͢Δ৔߹ɺwindowedؔ਺͸ඇৗʹศརͰ͢ɻ

Slide 102

Slide 102 text

5.2 windowed ύλʔϯݕग़ ɾςΩετ΍਺ྻͷதͰಛఆͷύλʔϯΛݕग़͢ΔࡍʹɺwindowedΛ࢖ͬͯ࿈ ଓ͢Δ෦෼ΛνΣοΫͰ͖·͢ɻ

Slide 103

Slide 103 text

ࠩ෼ͷܭࢉ 5.2 windowed ɾ਺ྻͷྡ઀͢Δཁૉؒͷࠩ෼Λܭࢉ͢Δ৔߹ʹ΋windowed͕໾ཱͪ·͢ɻ

Slide 104

Slide 104 text

Ի੠ͷղੳ 5.2 windowed

Slide 105

Slide 105 text

ύϑΥʔϚϯεͷ࠷దԽ sequence forจ 06.

Slide 106

Slide 106 text

6.1 Sequenceͱforจ ɾSequence͸஗ԆධՁΛߦ͏Collection typeͰେྔͷσʔλॲཧʹ͓͍ͯޮ཰తͰ͢ ɾSequence͸ཁૉΛҰͭͣͭॲཧ͠ɺதؒૢ࡞͸஗Ԇ͞Εɺ࠷ऴૢ࡞͕ݺ͹ΕΔ·Ͱ࣮ߦ ͞Ε·ͤΜɻ͜ΕʹΑΓɺதؒૢ࡞ͷແବͳܭࢉΛආ͚Δ͜ͱ͕Ͱ͖·͢ɻ Sequence

Slide 107

Slide 107 text

ύϑΥʔϚϯεൺֱɹCollection, Sequence, for IntegerͷϦετʹରͯ͠Լهͷ࣮ߦΛߦ͍·͢ɻ IntegerͷϦετͷαΠζ͸ෳ਺࣮ࢪɻ ɾۮ਺ͷΈΛநग़ɻ ɾ͞Βʹɺͦͷத͔Β3ͰׂΓ੾ΕΔ਺Λநग़ʢ6ͷഒ਺ʣɻ ɾͦΕΒͷ਺Λೋ৐ɻ ɾ࠷ॳͷ5ݸͷཁૉΛऔΓग़͢ɻ 6.1 Sequenceͱforจ

Slide 108

Slide 108 text

Collection 6.1 Sequenceͱforจ

Slide 109

Slide 109 text

Sequence 6.1 Sequenceͱforจ

Slide 110

Slide 110 text

for 6.1 Sequenceͱforจ

Slide 111

Slide 111 text

ɾSequence͸ϦετͷαΠζ͕૿͑ͯ΋࣮ߦ଎౓͸΄΅มΘΒͳ͍͕ɺϦετ͕খͯ͘͞΋overhead͕ଘࡏͯ͠͠·͏ ɾCollection͸ϦετͷαΠζ͕૿͑Δͱൺྫͯ͠૿͑ΔɻϦετ͕େ͖͘ͳ͍࣌͸SequenceΑΓ΋ૣ͍ ɾforจ͸࠷଎ 6.1 Sequenceͱforจ Kotlin PlaygroundͰͷ10ճฏۉ

Slide 112

Slide 112 text

SequenceͱCollectionͷҧ͍ ɾCollection: ଈ࣌ධՁΛߦ͍ɺશͯͷཁૉΛϝϞϦʹอ࣋͠ɺॲཧ͠·͢ɻ ɾSequence: ஗ԆධՁΛߦ͍ɺඞཁͳཁૉͷΈΛॲཧ͠·͢ɻ 6.1 Sequenceͱforจ

Slide 113

Slide 113 text

Sequenceͷૢ࡞ தؒૢ࡞: ऴ୺ૢ࡞: தؒૢ࡞͸஗ԆධՁ͞ΕΔૢ࡞Ͱ͢ɻྫ͑͹ɺmapɺfilterɺͳ Ͳؚ͕·Ε·͢ɻ͜ΕΒͷૢ࡞͸৽͍͠γʔέϯεΛฦ͠ɺ࣮ࡍ ͷॲཧ͸ऴ୺ૢ࡞͕ݺͼग़͞ΕΔ·ͰߦΘΕ·ͤΜɻ ऴ୺ૢ࡞͸SequenceΛফඅͯ݁͠ՌΛੜ੒͢Δૢ࡞Ͱ͢ɻྫ͑ ͹ɺtoListɺsumɺcountɺfirstͳͲ͕͋Γ·͢ɻऴ୺ૢ࡞͕ݺ͹ ΕΔͱɺશͯͷதؒૢ࡞͕࣮ߦ͞Ε·͢ɻ 6.1 Sequenceͱforจ

Slide 114

Slide 114 text

Collection 6.1 Sequenceͱforจ

Slide 115

Slide 115 text

Collection 6.1 Sequenceͱforจ

Slide 116

Slide 116 text

Sequence 6.1 Sequenceͱforจ

Slide 117

Slide 117 text

Sequence 6.1 Sequenceͱforจ

Slide 118

Slide 118 text

Sequence 6.1 Sequenceͱforจ ऴ୺ͷiterateͰCollectionͷཁૉ͝ͱʹॲཧΛ͍ͯ͠·͢ɻ

Slide 119

Slide 119 text

Sequenceͷ࢖͍ॴ 1. ͔ͳΓେن໛ͳίϨΫγϣϯʹରͯ͠ෳ਺ͷॲཧΛߦ͏৔߹ 2. ଟஈνΣʔϯΛల։͠ɺதؒϦετͷੜ੒Λආ͚͍ͨ৔߹ 3. ύϑΥʔϚϯεͱϝϞϦ࢖༻ྔΛ࠷దԽ͍͕ͨ͠ɺforจΛ࢖͍ͨ͘ͳ͍৔߹ 6.1 Sequenceͱforจ

Slide 120

Slide 120 text

6.2 ·ͱΊ ɾCollectionؔ਺͸༏लͳͷͰɺجຊతʹCollectionؔ਺Λ࢖͍·͠ΐ͏ɻ ɾϦετͷαΠζ͕େ͖͘ύϑΥʔϚϯεͷϘτϧωοΫ͕͋ΓɺՄಡੑΛ ҡ͍࣋ͨ͠৔߹ʹSequenceΛ࢖༻ ɾ࠷଎ΛٻΊΔͳΒforจ

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

End Of doc.