Kotlin極簡(jiǎn)教程:第5章 集合類(lèi)(上)

原文鏈接:https://github.com/EasyKotlin

本章將介紹Kotlin標(biāo)準(zhǔn)庫(kù)中的集合類(lèi),我們將了解到它是如何擴(kuò)展的Java集合庫(kù),使得寫(xiě)代碼更加簡(jiǎn)單容易。如果您熟悉Scala的集合庫(kù),您會(huì)發(fā)現(xiàn)Kotlin跟Scala集合類(lèi)庫(kù)的相似之處。

5.1 集合類(lèi)是什么

5.1.2 集合類(lèi)是一種數(shù)據(jù)結(jié)構(gòu)

在講 Kotlin 的集合類(lèi)之前,為了更加深刻理解為什么要有集合類(lèi),以及集合類(lèi)到底是怎么一回事,讓我們先來(lái)簡(jiǎn)單回顧一下編程的本質(zhì):

數(shù)據(jù)結(jié)構(gòu) + 算法 (信息的邏輯結(jié)構(gòu)及其基本操作)

我們使用計(jì)算機(jī)編程來(lái)解決一個(gè)具體問(wèn)題時(shí),大致需要經(jīng)過(guò)下列幾個(gè)步驟:

首先要從具體問(wèn)題中抽象出一個(gè)適當(dāng)?shù)臄?shù)學(xué)模型;
然后設(shè)計(jì)一個(gè)解此數(shù)學(xué)模型的算法(Algorithm);
最后編出程序、進(jìn)行測(cè)試、修改直至得到最終解答。

這里面的尋求數(shù)學(xué)模型的過(guò)程,實(shí)質(zhì)就是分析問(wèn)題,從中提取操作的對(duì)象,并找出這些操作對(duì)象之間含有的關(guān)系的過(guò)程。建立好的模型,我們使用數(shù)學(xué)語(yǔ)言來(lái)表達(dá)。

這里的模型對(duì)應(yīng)的就是數(shù)據(jù)結(jié)構(gòu)。我們用計(jì)算機(jī)編程來(lái)解決問(wèn)題的關(guān)鍵就是,設(shè)計(jì)出合適的數(shù)據(jù)結(jié)構(gòu)(例如,用線性表、樹(shù)、圖等)和性能良好的算法。

算法與數(shù)據(jù)的結(jié)構(gòu)密切相關(guān),算法無(wú)不依附于具體的數(shù)據(jù)結(jié)構(gòu),數(shù)據(jù)結(jié)構(gòu)直接關(guān)系到算法的選擇和效率。通常情況下,設(shè)計(jì)良好的數(shù)據(jù)結(jié)構(gòu)可以大大簡(jiǎn)化算法的實(shí)現(xiàn)復(fù)雜度,同時(shí)可以提升存儲(chǔ)效率。數(shù)據(jù)結(jié)構(gòu)往往同高效的檢索算法和索引技術(shù)相關(guān)。

我們可以把數(shù)據(jù)結(jié)構(gòu)理解為是ADT的實(shí)現(xiàn)。數(shù)據(jù)結(jié)構(gòu)就是現(xiàn)實(shí)問(wèn)題模型的表達(dá)。

數(shù)據(jù)結(jié)構(gòu)主要解決以下三個(gè)問(wèn)題:

  • 數(shù)據(jù)元素之間的邏輯關(guān)系。

這些邏輯關(guān)系有:集合、線性結(jié)構(gòu)、樹(shù)形結(jié)構(gòu)、圖形結(jié)構(gòu)等。

  • 數(shù)據(jù)的物理結(jié)構(gòu)。

數(shù)據(jù)的邏輯結(jié)構(gòu)在計(jì)算機(jī)存儲(chǔ)空間的存放形式。數(shù)據(jù)的物理結(jié)構(gòu)是數(shù)據(jù)結(jié)構(gòu)在計(jì)算機(jī)中的映射。其具體實(shí)現(xiàn)的方法有: 順序(Sequence)、鏈接(Link)、索引(Index)、散列(Hash)等形式。

其中,順序存儲(chǔ)結(jié)構(gòu)和鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)是我們常用的兩種存儲(chǔ)結(jié)構(gòu)。

順序存儲(chǔ)是使用元素在存儲(chǔ)器中的相對(duì)位置來(lái)表示數(shù)據(jù)元素之間的邏輯關(guān)系;

鏈?zhǔn)酱鎯?chǔ)使用指示元素存儲(chǔ)位置的指針(pointer)來(lái)表示數(shù)據(jù)元素之間的邏輯關(guān)系。

  • 數(shù)據(jù)的處理運(yùn)算。

5.1.2 集合類(lèi)是SDK API

我們現(xiàn)在很少用抽象數(shù)據(jù)類(lèi)型ADT(Abstract Data Type)這個(gè)概念,其實(shí)這個(gè)概念是OO范式的前身,也是類(lèi)的前身。ADT加上繼承、重載和多態(tài)性就是現(xiàn)代OOP編程范式中的類(lèi)的概念了。我們簡(jiǎn)稱(chēng)類(lèi)為廣義ADT的概念。

如果我們更加廣義的來(lái)理解這里的ADT的思想,其實(shí)各種編程語(yǔ)言的SDK API、所有的服務(wù)(IaaS,PaaS和SaaS等)都是一種更加廣義的ADT。

使用ADT可以讓我們更簡(jiǎn)單地描述現(xiàn)實(shí)世界。例如:用線性表描述學(xué)生成績(jī)表,用樹(shù)或圖描述遺傳關(guān)系等。

我們知道類(lèi)的本質(zhì)就是,對(duì)象及其關(guān)系的抽象(abstraction)。一個(gè)類(lèi)通常有屬性(數(shù)據(jù)結(jié)構(gòu))和行為(算法)。使用OO范式編程的大致過(guò)程為:

劃分對(duì)象 → 抽象類(lèi) → 將類(lèi)組織成為層次化結(jié)構(gòu)(繼承和合成) → 用類(lèi)與實(shí)例進(jìn)行設(shè)計(jì)和實(shí)現(xiàn)

等幾個(gè)階段。

數(shù)據(jù)抽象本質(zhì)上講就是我們解決現(xiàn)實(shí)問(wèn)題的過(guò)程中,進(jìn)行建立領(lǐng)域模型(Domain Model)的過(guò)程。

比如說(shuō),在前一章節(jié)中,我們介紹的程序設(shè)計(jì)語(yǔ)言的類(lèi)型系統(tǒng),本質(zhì)上就是一種數(shù)據(jù)抽象。由于計(jì)算機(jī)的結(jié)構(gòu)和存儲(chǔ)的限制(無(wú)法像人類(lèi)大腦神經(jīng)系統(tǒng)一樣去認(rèn)知識(shí)別,并解決現(xiàn)實(shí)問(wèn)題),人類(lèi)大腦在解決實(shí)際問(wèn)題過(guò)程中,經(jīng)常要計(jì)算整數(shù)、小數(shù), 要處理英文字符、中文字符, 要持有對(duì)象(被操作的數(shù)據(jù)),要對(duì)這些對(duì)象進(jìn)行諸如:查找、排序、修改、傳遞等操作。把這些問(wèn)題解決中最常用的數(shù)據(jù)結(jié)構(gòu)以及其操作算法抽象成對(duì)應(yīng)的類(lèi)(例如:String、Array、List、Set、Map等),這樣我們就可以極大的復(fù)用這些功能。而不需要我們自己來(lái)實(shí)現(xiàn)諸如:字符串、數(shù)組、列表、集合、映射等這些的數(shù)據(jù)結(jié)構(gòu)。通常這些最通用的數(shù)據(jù)結(jié)構(gòu),都是現(xiàn)在編程語(yǔ)言中內(nèi)置的了。

5.1.3 連續(xù)存儲(chǔ)和離散存儲(chǔ)

內(nèi)存中的存儲(chǔ)形式可以分為連續(xù)存儲(chǔ)和離散存儲(chǔ)兩種。因此,數(shù)據(jù)的物理存儲(chǔ)結(jié)構(gòu)就有連續(xù)存儲(chǔ)和離散存儲(chǔ)兩種,它們對(duì)應(yīng)了我們通常所說(shuō)的數(shù)組和鏈表。

由于數(shù)組是連續(xù)存儲(chǔ)的,在操作數(shù)組中的數(shù)據(jù)時(shí)就可以根據(jù)離首地址的偏移量直接存取相應(yīng)位置上的數(shù)據(jù),但是如果要在數(shù)據(jù)組中任意位置上插入一個(gè)元素,就需要先把后面的元素集體向后移一位為其空出存儲(chǔ)空間。與之相反,鏈表是離散存儲(chǔ)的,所以在插入一個(gè)數(shù)據(jù)時(shí)只要申請(qǐng)一片新空間,然后將其中的連接關(guān)系做一個(gè)修改就可以,但是顯然在鏈表上查找一個(gè)數(shù)據(jù)時(shí)就要逐個(gè)遍歷了。

考慮以上的總結(jié)可見(jiàn),數(shù)組和鏈表各有優(yōu)缺點(diǎn)。在具體使用時(shí)要根據(jù)具體情況選擇。當(dāng)查找數(shù)據(jù)操作比較多時(shí)最好用數(shù)組;當(dāng)對(duì)數(shù)據(jù)集中的數(shù)據(jù)進(jìn)行添加或刪除比較多時(shí)最好選擇鏈表。

5.2 Kotlin 集合類(lèi)簡(jiǎn)介

集合類(lèi)存放的都是對(duì)象的引用,而非對(duì)象本身,我們通常說(shuō)的集合中的對(duì)象指的是集合中對(duì)象的引用(reference)。

Kotlin的集合類(lèi)分為:可變集合類(lèi)(Mutable)與不可變集合類(lèi)(Immutable)。

集合類(lèi)型主要有3種:list(列表)、set(集)、和 map(映射)。

(1)列表

列表的主要特征是其對(duì)象以線性方式存儲(chǔ),沒(méi)有特定順序,只有一個(gè)開(kāi)頭和一個(gè)結(jié)尾,當(dāng)然,它與根本沒(méi)有順序的集是不同的。

列表在數(shù)據(jù)結(jié)構(gòu)中可表現(xiàn)為:數(shù)組和向量、鏈表、堆棧、隊(duì)列等。

(2)集

集(set)是最簡(jiǎn)單的一種集合,它的對(duì)象不按特定方式排序,只是簡(jiǎn)單的把對(duì)象加入集合中,就像往口袋里放東西。

對(duì)集中成員的訪問(wèn)和操作是通過(guò)集中對(duì)象的引用進(jìn)行的,所以集中不能有重復(fù)對(duì)象。

集也有多種變體,可以實(shí)現(xiàn)排序等功能,如TreeSet,它把對(duì)象添加到集中的操作將變?yōu)榘凑漳撤N比較規(guī)則將其插入到有序的對(duì)象序列中。它實(shí)現(xiàn)的是SortedSet接口,也就是加入了對(duì)象比較的方法。通過(guò)對(duì)集中的對(duì)象迭代,我們可以得到一個(gè)升序的對(duì)象集合。

(3)映射

映射與集或列表有明顯區(qū)別,映射中每個(gè)項(xiàng)都是成對(duì)的。映射中存儲(chǔ)的每個(gè)對(duì)象都有一個(gè)相關(guān)的關(guān)鍵字(Key)對(duì)象,關(guān)鍵字決定了 對(duì)象在映射中的存儲(chǔ)位置,檢索對(duì)象時(shí)必須提供相應(yīng)的關(guān)鍵字,就像在字典中查單詞一樣。關(guān)鍵字應(yīng)該是唯一的。

關(guān)鍵字本身并不能決定對(duì)象的存儲(chǔ)位置,它需要對(duì)過(guò)一種散列(hashing)技術(shù)來(lái)處理,產(chǎn)生一個(gè)被稱(chēng)作散列碼(hash code)的整數(shù)值,

散列碼通常用作一個(gè)偏置量,該偏置量是相對(duì)于分配給映射的內(nèi)存區(qū)域起始位置的,由此確定關(guān)鍵字/對(duì)象對(duì)的存儲(chǔ)位置。理想情況 下,散列處理應(yīng)該產(chǎn)生給定范圍內(nèi)均勻分布的值,而且每個(gè)關(guān)鍵字應(yīng)得到不同的散列碼。

5.3 List

List接口繼承于Collection接口,元素以線性方式存儲(chǔ),集合中可以存放重復(fù)對(duì)象。Kotlin的List分為:不可變集合類(lèi)List(ReadOnly, Immutable)和可變集合類(lèi)MutableList(Read&Write, Mutable)。

其類(lèi)圖結(jié)構(gòu)如下:

img
img

其中,Iterator是所有容器類(lèi)Collection的迭代器。迭代器(Iterator)模式,又叫做游標(biāo)(Cursor)模式。GOF給出的定義為:提供一種方法訪問(wèn)一個(gè)容器對(duì)象中各個(gè)元素,而又不需暴露該對(duì)象的內(nèi)部細(xì)節(jié)。 從定義可見(jiàn),迭代器模式是為容器而生。

5.3.1 創(chuàng)建不可變List

我們可以使用listOf函數(shù)來(lái)構(gòu)建一個(gè)不可變的List(read-only,只讀的List)。它定義在libraries/stdlib/src/kotlin/collections/Collections.kt 里面。關(guān)于listOf這個(gè)構(gòu)建函數(shù)有下面3個(gè)重載函數(shù):

@kotlin.internal.InlineOnly
public inline fun <T> listOf(): List<T> = emptyList()

public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()

@JvmVersion
public fun <T> listOf(element: T): List<T> = java.util.Collections.singletonList(element)

這些函數(shù)創(chuàng)建的List都是是只讀的(readonly,也就是不可變的immutable )、可序列化的。

其中,

  • listOf()用于創(chuàng)建沒(méi)有元素的空List
  • listOf(vararg elements: T)用于創(chuàng)建只有一個(gè)元素的List
  • listOf(element: T)用于創(chuàng)建擁有多個(gè)元素的List

我們使用代碼示例分別來(lái)演示其用法:

首先,我們使用listOf()來(lái)構(gòu)建一個(gè)沒(méi)有元素的空的List:

>>> val list:List<Int> = listOf()
>>> list
[]
>>> list::class
class kotlin.collections.EmptyList

注意,這里的變量的類(lèi)型不能省略,否則會(huì)報(bào)錯(cuò):

>>> val list = listOf()
error: type inference failed: Not enough information to infer parameter T in inline fun <T> listOf(): List<T>
Please specify it explicitly.

val list = listOf()
           ^

因?yàn)檫@是一個(gè)泛型函數(shù)。關(guān)于泛型,我們將在下一章中介紹。

其中,EmptyList 是一個(gè) internal object EmptyList, 這是Kotlin內(nèi)部定義的一個(gè)默認(rèn)空的object List類(lèi)。

下面,我們?cè)賮?lái)創(chuàng)建一個(gè)只有1個(gè)元素的List:

>>> val list = listOf(1)
>>> list::class
class java.util.Collections$SingletonList

我們可以看出,它實(shí)際上是調(diào)用Java的java.util.Collections 里面的singletonList方法:

public static <T> List<T> singletonList(T o) {
    return new SingletonList<>(o);
}

我們?cè)賮?lái)創(chuàng)建一個(gè)有多個(gè)元素的List:

>>> val list = listOf(0,1, 2, 3, 4, 5, 6,7,8,9)
>>> list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list::class
class java.util.Arrays$ArrayList
>>> list::class.java

它調(diào)用的是

fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()

這個(gè)函數(shù)。其中,asList函數(shù)是Array的擴(kuò)展函數(shù):

public fun <T> Array<out T>.asList(): List<T> {
    return ArraysUtilJVM.asList(this)
}

而這個(gè)ArraysUtilJVM是一個(gè)Java類(lèi),里面實(shí)際上調(diào)用的是java.util.Arraysjava.util.List :

package kotlin.collections;

import java.util.Arrays;
import java.util.List;

class ArraysUtilJVM {
    static <T> List<T> asList(T[] array) {
        return Arrays.asList(array);
    }
}

另外,我們還可以直接使用arrayListOf函數(shù)來(lái)創(chuàng)建一個(gè)Java中的ArrayList對(duì)象實(shí)例:

>>> val list = arrayListOf(0,1,2,3)
>>> list
[0, 1, 2, 3]
>>> list::class
class java.util.ArrayList
>>> val list = listOf(0,1, 2, 3, 4, 5, 6,7,8,9) 
>>> list::class
class java.util.Arrays$ArrayList

這個(gè)函數(shù)定義在libraries/stdlib/src/kotlin/collections/Collections.kt類(lèi)中:

@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public inline fun <T> arrayListOf(): ArrayList<T> = ArrayList()

同樣的處理方式,這里的ArrayList()是Java中的java.util.ArrayList的類(lèi)型別名:

@SinceKotlin("1.1") public typealias ArrayList<E> = java.util.ArrayList<E>

5.3.2 創(chuàng)建可變集合MutableList

在MutableList中,除了繼承List中的那些函數(shù)外,另外新增了add/addAll、remove/removeAll/removeAt、set、clear、retainAll等更新修改的操作函數(shù)。

override fun add(element: E): Boolean
override fun remove(element: E): Boolean
override fun addAll(elements: Collection<E>): Boolean
fun addAll(index: Int, elements: Collection<E>): Boolean
override fun removeAll(elements: Collection<E>): Boolean
override fun retainAll(elements: Collection<E>): Boolean
override fun clear(): Unit
operator fun set(index: Int, element: E): E
fun add(index: Int, element: E): Unit
fun removeAt(index: Int): E
override fun listIterator(): MutableListIterator<E>
override fun listIterator(index: Int): MutableListIterator<E>
override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>

創(chuàng)建一個(gè)MutableList的對(duì)象實(shí)例跟List類(lèi)似,前面加上前綴mutable,代碼示例如下:

>>> val list = mutableListOf(1, 2, 3)
>>> list
[1, 2, 3]
>>> list::class
class java.util.ArrayList
>>> val list2 = mutableListOf<Int>()
>>> list2
[]
>>> list2::class
class java.util.ArrayList
>>> val list3 = mutableListOf(1)
>>> list3
[1]
>>> list3::class
class java.util.ArrayList

我們可以看出,使用mutableListOf函數(shù)創(chuàng)建的可變集合類(lèi),實(shí)際上背后調(diào)用的是java.util.ArrayList類(lèi)的相關(guān)方法。

另外,我們可以直接使用Kotlin封裝的arrayListOf函數(shù)來(lái)創(chuàng)建一個(gè)ArrayList:

>>> val list4 = arrayListOf(1, 2, 3)
>>> list4::class
class java.util.ArrayList

關(guān)于Kotlin中的ArrayList類(lèi)型別名定義在
kotlin/collections/TypeAliases.kt 文件中:

@file:kotlin.jvm.JvmVersion

package kotlin.collections

@SinceKotlin("1.1") public typealias RandomAccess = java.util.RandomAccess


@SinceKotlin("1.1") public typealias ArrayList<E> = java.util.ArrayList<E>
@SinceKotlin("1.1") public typealias LinkedHashMap<K, V> = java.util.LinkedHashMap<K, V>
@SinceKotlin("1.1") public typealias HashMap<K, V> = java.util.HashMap<K, V>
@SinceKotlin("1.1") public typealias LinkedHashSet<E> = java.util.LinkedHashSet<E>
@SinceKotlin("1.1") public typealias HashSet<E> = java.util.HashSet<E>


// also @SinceKotlin("1.1")
internal typealias SortedSet<E> = java.util.SortedSet<E>
internal typealias TreeSet<E> = java.util.TreeSet<E>

如果我們已經(jīng)有了一個(gè)不可變的List,而我們現(xiàn)在想把他轉(zhuǎn)換成可變的List,我們可以直接調(diào)用轉(zhuǎn)換函數(shù)toMutableList

val list = mutableListOf(1, 2, 3)
val mlist = list.toMutableList()
mlist.add(5)

5.3.3 遍歷List元素

使用Iterator迭代器

我們以集合 val list = listOf(0,1, 2, 3, 4, 5, 6,7,8,9)為例,使用Iterator迭代器遍歷列表所有元素的操作:

>>> val list = listOf(0,1, 2, 3, 4, 5, 6,7,8,9) 
>>> list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> val iterator = list.iterator()
>>> iterator
java.util.AbstractList$Itr@438bad7c
>>> while(iterator.hasNext()){
... println(iterator.next())
... }
0
1
2
3
4
5
6
7
8
9

迭代器是一種設(shè)計(jì)模式,它是一個(gè)對(duì)象,它可以遍歷并選擇序列中的對(duì)象,而開(kāi)發(fā)人員不需要了解該序列的底層結(jié)構(gòu)。迭代器通常被稱(chēng)為“輕量級(jí)”對(duì)象,因?yàn)閯?chuàng)建它的代價(jià)小。

Kotlin中的Iterator功能比較簡(jiǎn)單,并且只能單向移動(dòng):

(1)調(diào)用iterator()函數(shù),容器返回一個(gè)Iterator實(shí)例。iterator()函數(shù)是kotlin.collections.Iterable中的函數(shù), 被Collection繼承。
(2)調(diào)用hasNext()函數(shù)檢查序列中是否還有元素。
(3)第一次調(diào)用Iterator的next()函數(shù)時(shí),它返回序列的第一個(gè)元素。依次向后遞推,使用next()獲得序列中的下一個(gè)元素。

當(dāng)我們調(diào)用到最后一個(gè)元素,再次調(diào)用next()函數(shù),會(huì)拋這個(gè)異常java.util.NoSuchElementException。代碼示例:

>>> val list = listOf(1,2,3)
>>> val iter = list.iterator()
>>> iter
java.util.AbstractList$Itr@3abfe845
>>> iter.hasNext()
true
>>> iter.next()
1
>>> iter.hasNext()
true
>>> iter.next()
2
>>> iter.hasNext()
true
>>> iter.next()
3
>>> iter.hasNext()
false
>>> iter.next()
java.util.NoSuchElementException
    at java.util.AbstractList$Itr.next(AbstractList.java:364)

我們可以看出,這里的Iterator的實(shí)現(xiàn)是在AbstractList中的內(nèi)部類(lèi)IteratorImpl

private open inner class IteratorImpl : Iterator<E> {
    protected var index = 0
    override fun hasNext(): Boolean = index < size
    override fun next(): E {
        if (!hasNext()) throw NoSuchElementException()
        return get(index++)
    }
}

通過(guò)這個(gè)實(shí)現(xiàn)源碼,我們可以更加清楚地明白Iterator的工作原理。

其中,NoSuchElementException()這個(gè)類(lèi)是java.util.NoSuchElementException的類(lèi)型別名:

@kotlin.SinceKotlin public typealias NoSuchElementException = java.util.NoSuchElementException

使用forEach遍歷List元素

這個(gè)forEach函數(shù)定義如下:

@kotlin.internal.HidesMembers
public inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)
}

它是package kotlin.collections包下面的Iterable的擴(kuò)展內(nèi)聯(lián)函數(shù)。它的入?yún)⑹且粋€(gè)函數(shù)類(lèi)型:

action: (T) -> Unit

關(guān)于函數(shù)式編程,我們將在后面章節(jié)中學(xué)習(xí)。

這里的forEach是一個(gè)語(yǔ)法糖。實(shí)際上forEach在遍歷List對(duì)象的時(shí)候,仍然使用的是iterator迭代器來(lái)進(jìn)行循環(huán)遍歷的。

>>> val list = listOf(1,2,3)
>>> list
[1, 2, 3]
>>> list.forEach{
... println(it)
... }
1
2
3

當(dāng)參數(shù)只有一個(gè)函數(shù)的時(shí)候,括號(hào)可以省略不寫(xiě)。

也就是說(shuō),這里面的forEach函數(shù)調(diào)用的寫(xiě)法,實(shí)際上跟下面的寫(xiě)法等價(jià):

list.forEach({
    println(it)
})

我們甚至還可以直接這樣寫(xiě):

>>> list.forEach(::println)

其中,:: 是函數(shù)引用符。

5.3.4 List元素操作函數(shù)

add remove set clear

這兩個(gè)添加、刪除操作函數(shù)是MutableList里面的。跟Java中的集合類(lèi)操作類(lèi)似。

創(chuàng)建一個(gè)可變集合:

>>> val mutableList = mutableListOf(1,2,3)
>>> mutableList
[1, 2, 3]

向集合中添加一個(gè)元素:

>>> mutableList.add(4)
true
>>> mutableList
[1, 2, 3, 4]

在下標(biāo)為0的位置添加元素0 :

>>> mutableList.add(0,0)
>>> mutableList
[0, 1, 2, 3, 4]

刪除元素1 :

>>> mutableList.remove(1)
true
>>> mutableList
[0, 2, 3, 4]
>>> mutableList.remove(1)
false

刪除下標(biāo)為1的元素:

>>> mutableList.removeAt(1)
2
>>> mutableList
[0, 3, 4]

刪除子集合:

>>> mutableList.removeAll(listOf(3,4))
true
>>> mutableList
[0]

添加子集合:

>>> mutableList.addAll(listOf(1,2,3))
true
>>> mutableList
[1, 2, 3]

更新設(shè)置下標(biāo)0的元素值為100:

>>> mutableList.set(0,100)
0
>>> mutableList
[100]

清空集合:

>>> mutableList.clear()
>>> mutableList
[]

把可變集合轉(zhuǎn)為不可變集合:

>>> mutableList.toList()
[1, 2, 3]

retainAll

取兩個(gè)集合交集:

>>> val mlist1 = mutableListOf(1,2,3,4,5,6)
>>> val mlist2 = mutableListOf(3,4,5,6,7,8,9)
>>> mlist1.retainAll(mlist2)
true
>>> mlist1
[3, 4, 5, 6]

contains(element: T): Boolean

判斷集合中是否有指定元素,有就返回true,否則返回false 。
代碼示例:

>>> val list = listOf(1,2,3,4,5,6,7)
>>> list.contains(1)
true

elementAt(index: Int): T

查找下標(biāo)對(duì)應(yīng)的元素,如果下標(biāo)越界會(huì)拋IndexOutOfBoundsException。
代碼示例:

>>> val list = listOf(1,2,3,4,5,6,7)
>>> list.elementAt(6)
7
>>> list.elementAt(7)
java.lang.ArrayIndexOutOfBoundsException: 7
    at java.util.Arrays$ArrayList.get(Arrays.java:3841)

另外,針對(duì)越界的處理,還有下面兩個(gè)函數(shù):

elementAtOrElse(index: Int, defaultValue: (Int) -> T): T : 查找下標(biāo)對(duì)應(yīng)元素,如果越界會(huì)根據(jù)方法返回默認(rèn)值。

>>> list.elementAtOrElse(7,{0})
0
>>> list.elementAtOrElse(7,{10})
10

elementAtOrNull(index: Int): T? : 查找下標(biāo)對(duì)應(yīng)元素,如果越界就返回null

>>> list.elementAtOrNull(7)
null

first()

返回集合第1個(gè)元素,如果是空集,拋出異常NoSuchElementException。

>>> val list = listOf(1,2,3)
>>> list.first()
1
>>> val emptyList = listOf<Int>()
>>> emptyList.first()
java.util.NoSuchElementException: List is empty.
    at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:178)


對(duì)應(yīng)的有針對(duì)異常處理的函數(shù)firstOrNull(): T? :

>>> emptyList.firstOrNull()
null

first(predicate: (T) -> Boolean): T

返回符合條件的第一個(gè)元素,沒(méi)有則拋異常NoSuchElementException 。

>>> val list = listOf(1,2,3)
>>> list.first({it%2==0})
2
>>> list.first({it>100})
java.util.NoSuchElementException: Collection contains no element matching the predicate.

對(duì)應(yīng)的有針對(duì)異常處理的函數(shù)firstOrNull(predicate: (T) -> Boolean): T? ,返回符合條件的第一個(gè)元素,沒(méi)有就返回null :

>>> list.firstOrNull({it>100})
null

indexOf(element: T): Int

返回指定下標(biāo)的元素,沒(méi)有就返回-1

>>> val list = listOf("a","b","c")
>>> list.indexOf("c")
2
>>> list.indexOf("x")
-1

indexOfFirst(predicate: (T) -> Boolean): Int

返回第一個(gè)符合條件的元素下標(biāo),沒(méi)有就返回-1 。

>>> val list = listOf("abc","xyz","xjk","pqk")
>>> list.indexOfFirst({it.contains("x")})
1
>>> list.indexOfFirst({it.contains("k")})
2
>>> list.indexOfFirst({it.contains("e")})
-1

indexOfLast(predicate: (T) -> Boolean): Int

返回最后一個(gè)符合條件的元素下標(biāo),沒(méi)有就返回-1 。

>>> val list = listOf("abc","xyz","xjk","pqk")
>>> list.indexOfLast({it.contains("x")})
2
>>> list.indexOfLast({it.contains("k")})
3
>>> list.indexOfLast({it.contains("e")})
-1

last()

返回集合最后一個(gè)元素,空集則拋出異常NoSuchElementException。

>>> val list = listOf(1,2,3,4,7,5,6,7,8)
>>> list.last()
8
>>> val emptyList = listOf<Int>()
>>> emptyList.last()
java.util.NoSuchElementException: List is empty.
    at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:340)

last(predicate: (T) -> Boolean): T

返回符合條件的最后一個(gè)元素,沒(méi)有就拋NoSuchElementException

>>> val list = listOf(1,2,3,4,7,5,6,7,8)
>>> list.last({it==7})
7
>>> list.last({it>10})
java.util.NoSuchElementException: List contains no element matching the predicate.

對(duì)應(yīng)的針對(duì)越界處理的lastOrNull函數(shù):返回符合條件的最后一個(gè)元素,沒(méi)有則返回null :

>>> list.lastOrNull({it>10})
null

lastIndexOf(element: T): Int

返回符合條件的最后一個(gè)元素,沒(méi)有就返回-1

>>> val list = listOf("abc","dfg","jkl","abc","bbc","wer")
>>> list.lastIndexOf("abc")
3

single(): T

該集合如果只有1個(gè)元素,則返回該元素。否則,拋異常。

>>> val list = listOf(1)
>>> list.single()
1

>>> val list = listOf(1,2)
>>> list.single()
java.lang.IllegalArgumentException: List has more than one element.
    at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:471)

>>> val list = listOf<Int>()

>>> list.single()
java.util.NoSuchElementException: List is empty.
    at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:469)

single(predicate: (T) -> Boolean): T

返回符合條件的單個(gè)元素,如有沒(méi)有符合的拋異常NoSuchElementException,或超過(guò)一個(gè)的拋異常IllegalArgumentException。

>>> val list = listOf(1,2,3,4,7,5,6,7,8)
>>> list.single({it==1})
1
>>> list.single({it==7})
java.lang.IllegalArgumentException: Collection contains more than one matching element.

>>> list.single({it==10})
java.util.NoSuchElementException: Collection contains no element matching the predicate.

對(duì)應(yīng)的針對(duì)異常處理的函數(shù)singleOrNull: 返回符合條件的單個(gè)元素,如有沒(méi)有符合或超過(guò)一個(gè),返回null

>>> list.singleOrNull({it==7})
null
>>> list.singleOrNull({it==10})
null

5.3.5 List集合類(lèi)的any all ``none ``count ``reduce ``fold ``max ``min sum 函數(shù)算子(operator)

any()判斷集合至少有一個(gè)元素

這個(gè)函數(shù)定義如下:

public fun <T> Iterable<T>.any(): Boolean {
    for (element in this) return true
    return false
}

如果該集合至少有一個(gè)元素,返回true,否則返回false

代碼示例:

>>> val emptyList = listOf<Int>()
>>> emptyList.any()
false
>>> val list1 = listOf(1)
>>> list1.any()
true

any(predicate: (T) -> Boolean) 判斷集合中是否有滿(mǎn)足條件的元素

這個(gè)函數(shù)定義如下:

public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {
    for (element in this) if (predicate(element)) return true
    return false
}

如果該集合中至少有一個(gè)元素匹配謂詞函數(shù)參數(shù)predicate: (T) -> Boolean,返回true,否則返回false。

代碼示例:

>>> val list = listOf(1, 2, 3)
>>> list.any() // 至少有1個(gè)元素
true
>>> list.any({it%2==0}) // 元素2滿(mǎn)足{it%2==0}
true
>>> list.any({it>4}) // 沒(méi)有元素滿(mǎn)足{it>4}
false

all(predicate: (T) -> Boolean) 判斷集合中的元素是否都滿(mǎn)足條件

函數(shù)定義:

public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
    for (element in this) if (!predicate(element)) return false
    return true
}

當(dāng)且僅當(dāng)該集合中所有元素都滿(mǎn)足條件時(shí),返回true;否則都返回false。

代碼示例:

>>> val list = listOf(0,2,4,6,8)
>>> list.all({it%2==0})
true
>>> list.all({it>2})
false

none()判斷集合無(wú)元素

函數(shù)定義:

public fun <T> Iterable<T>.none(): Boolean {
    for (element in this) return false
    return true
}

如果該集合沒(méi)有任何元素,返回true,否則返回false。

代碼示例:

>>> val list = listOf<Int>()
>>> list.none()
true

none(predicate: (T) -> Boolean)判斷集合中所有元素都不滿(mǎn)足條件

函數(shù)定義:

public inline fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean {
    for (element in this) if (predicate(element)) return false
    return true
}

當(dāng)且僅當(dāng)集合中所有元素都不滿(mǎn)足條件時(shí)返回true,否則返回false。

代碼示例:

>>> val list = listOf(0,2,4,6,8)
>>> list.none({it%2==1})
true
>>> list.none({it>0})
false

count() 計(jì)算集合中元素的個(gè)數(shù)

函數(shù)定義:

public fun <T> Iterable<T>.count(): Int {
    var count = 0
    for (element in this) count++
    return count
}

代碼示例:

>>> val list = listOf(0,2,4,6,8,9)
>>> list.count()
6

count(predicate: (T) -> Boolean) 計(jì)算集合中滿(mǎn)足條件的元素的個(gè)數(shù)

函數(shù)定義:

public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Int {
    var count = 0
    for (element in this) if (predicate(element)) count++
    return count
}

代碼示例:

>>> val list = listOf(0,2,4,6,8,9)
>>> list.count()
6
>>> list.count({it%2==0})
5

reduce從 第一項(xiàng)到最后一項(xiàng)進(jìn)行累計(jì)運(yùn)算

函數(shù)定義:

public inline fun <S, T: S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S {
    val iterator = this.iterator()
    if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.")
    var accumulator: S = iterator.next()
    while (iterator.hasNext()) {
        accumulator = operation(accumulator, iterator.next())
    }
    return accumulator
}

首先把第一個(gè)元素賦值給累加子accumulator,然后逐次向后取元素累加,新值繼續(xù)賦值給累加子accumulator = operation(accumulator, iterator.next()),以此類(lèi)推。最后返回累加子的值。

代碼示例:

>>> val list = listOf(1,2,3,4,5,6,7,8,9)
>>> list.reduce({sum, next->sum+next})
45
>>> list.reduce({sum, next->sum*next})
362880
>>> val list = listOf("a","b","c")
>>> list.reduce({total, s->total+s})
abc

reduceRight從最后一項(xiàng)到第一項(xiàng)進(jìn)行累計(jì)運(yùn)算

函數(shù)定義:

public inline fun <S, T: S> List<T>.reduceRight(operation: (T, acc: S) -> S): S {
    val iterator = listIterator(size)
    if (!iterator.hasPrevious())
        throw UnsupportedOperationException("Empty list can't be reduced.")
    var accumulator: S = iterator.previous()
    while (iterator.hasPrevious()) {
        accumulator = operation(iterator.previous(), accumulator)
    }
    return accumulator
}

從函數(shù)的定義accumulator = operation(iterator.previous(), accumulator), 我們可以看出,從右邊累計(jì)運(yùn)算的累加子是放在后面的。

代碼示例:

>>> val list = listOf("a","b","c")
>>> list.reduceRight({total, s -> s+total})
cba

如果我們位置放錯(cuò)了,會(huì)輸出下面的結(jié)果:

>>> list.reduceRight({total, s -> total+s})
abc

fold(initial: R, operation: (acc: R, T) -> R): R 帶初始值的reduce

函數(shù)定義:

public inline fun <T, R> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> R): R {
    var accumulator = initial
    for (element in this) accumulator = operation(accumulator, element)
    return accumulator
}

從函數(shù)的定義,我們可以看出,fold函數(shù)給累加子賦了初始值initial。

代碼示例:

>>> val list=listOf(1,2,3,4)
>>> list.fold(100,{total, next -> next + total})
110

foldRightreduceRight類(lèi)似,有初始值。

函數(shù)定義:

public inline fun <T, R> List<T>.foldRight(initial: R, operation: (T, acc: R) -> R): R {
    var accumulator = initial
    if (!isEmpty()) {
        val iterator = listIterator(size)
        while (iterator.hasPrevious()) {
            accumulator = operation(iterator.previous(), accumulator)
        }
    }
    return accumulator
}

代碼示例:

>>> val list = listOf("a","b","c")
>>> list.foldRight("xyz",{s, pre -> pre + s})
xyzcba

forEach(action: (T) -> Unit): Unit 循環(huán)遍歷元素,元素是it

我們?cè)谇拔囊呀?jīng)講述,參看5.3.4。

再寫(xiě)個(gè)代碼示例:

>>> val list = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 
>>> list.forEach { value -> if (value > 7) println(value) } 
8
9

forEachIndexed 帶index(下標(biāo)) 的元素遍歷

函數(shù)定義:

public inline fun <T> Iterable<T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit {
    var index = 0
    for (item in this) action(index++, item)
}

代碼示例:

>>> val list = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 
>>> list.forEachIndexed { index, value -> if (value > 8) println("value of index $index is $value, greater than 8") } 

value of index 9 is 9, greater than 8

maxmin查詢(xún)最大、最小的元素,空集則返回null

max函數(shù)定義:

public fun <T : Comparable<T>> Iterable<T>.max(): T? {
    val iterator = iterator()
    if (!iterator.hasNext()) return null
    var max = iterator.next()
    while (iterator.hasNext()) {
        val e = iterator.next()
        if (max < e) max = e
    }
    return max
}

返回集合中最大的元素。

代碼示例:

>>> val list = listOf(1,2,3)
>>> list.max()
3
>>> val list = listOf("a","b","c")
>>> list.max()
c

min函數(shù)定義:

public fun <T : Comparable<T>> Iterable<T>.min(): T? {
    val iterator = iterator()
    if (!iterator.hasNext()) return null
    var min = iterator.next()
    while (iterator.hasNext()) {
        val e = iterator.next()
        if (min > e) min = e
    }
    return min
}

返回集合中的最小元素。

代碼示例:

>>> val list = listOf(1,2,3)
>>> list.min()
1
>>> val list = listOf("a","b","c")
>>> list.min()
a

在Kotlin中,字符串的大小比較比較有意思的,我們直接通過(guò)代碼示例來(lái)學(xué)習(xí)一下:

>>> "c" > "a"
true
>>> "abd" > "abc"
true
>>> "abd" > "abcd"
true
>>> "abd" > "abcdefg"
true

我們可以看出,字符串的大小比較是按照對(duì)應(yīng)的下標(biāo)的字符進(jìn)行比較的。
另外,布爾值的比較是true大于false

>>> true > false
true

maxBy(selector: (T) -> R): T? 、 minBy(selector: (T) -> R): T?獲取函數(shù)映射結(jié)果的最大值、最小值對(duì)應(yīng)的那個(gè)元素的值,如果沒(méi)有則返回null

函數(shù)定義:

public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T? {
    val iterator = iterator()
    if (!iterator.hasNext()) return null
    var maxElem = iterator.next()
    var maxValue = selector(maxElem)
    while (iterator.hasNext()) {
        val e = iterator.next()
        val v = selector(e)
        if (maxValue < v) {
            maxElem = e
            maxValue = v
        }
    }
    return maxElem
}

也就是說(shuō),不直接比較集合元素的大小,而是以集合元素為入?yún)⒌暮瘮?shù)selector: (T) -> R 返回值來(lái)比較大小,最后返回此元素的值(注意,不是對(duì)應(yīng)的selector函數(shù)的返回值)。有點(diǎn)像數(shù)學(xué)里的求函數(shù)最值問(wèn)題:

給定函數(shù) y = f(x) , 求max f(x)x的值。

代碼示例:

>>> val list = listOf(100,-500,300,200)
>>> list.maxBy({it})
300
>>> list.maxBy({it*(1-it)})
100
>>> list.maxBy({it*it})
-500

對(duì)應(yīng)的 minBy 是獲取函數(shù)映射后返回結(jié)果的最小值所對(duì)應(yīng)那個(gè)元素的值,如果沒(méi)有則返回null。

代碼示例:

>>> val list = listOf(100,-500,300,200)
>>> list.minBy({it})
-500
>>> list.minBy({it*(1-it)})
-500
>>> list.minBy({it*it})
100

sumBy(selector: (T) -> Int): Int 獲取函數(shù)映射值的總和

函數(shù)定義:

public inline fun <T> Iterable<T>.sumBy(selector: (T) -> Int): Int {
    var sum: Int = 0
    for (element in this) {
        sum += selector(element)
    }
    return sum
}

可以看出,這個(gè)sumBy函數(shù)算子,累加器sum初始值為0,返回值是Int。它的入?yún)?code>selector是一個(gè)函數(shù)類(lèi)型(T) -> Int,也就是說(shuō)這個(gè)selector也是返回Int類(lèi)型的函數(shù)。

代碼示例:

>>> val list = listOf(1,2,3,4)
>>> list.sumBy({it})
10
>>> list.sumBy({it*it})
30

類(lèi)型錯(cuò)誤反例:

>>> val list = listOf("a","b","c")
>>> list.sumBy({it})
error: type inference failed: inline fun <T> Iterable<T>.sumBy(selector: (T) -> Int): Int
cannot be applied to
receiver: List<String>  arguments: ((String) -> String)

list.sumBy({it})
     ^
error: type mismatch: inferred type is (String) -> String but (String) -> Int was expected
list.sumBy({it})
           ^

5.3.6 過(guò)濾操作函數(shù)算子

take(n: Int): List<T> 挑出該集合前n個(gè)元素的子集合

函數(shù)定義:

public fun <T> Iterable<T>.take(n: Int): List<T> {
    require(n >= 0) { "Requested element count $n is less than zero." }
    if (n == 0) return emptyList()
    if (this is Collection<T>) {
        if (n >= size) return toList()
        if (n == 1) return listOf(first())
    }
    var count = 0
    val list = ArrayList<T>(n)
    for (item in this) {
        if (count++ == n)
            break
        list.add(item)
    }
    return list.optimizeReadOnlyList()
}

如果n等于0,返回空集;如果n大于集合size,返回該集合。

代碼示例:

>>> val list = listOf("a","b","c")
>>> list
[a, b, c]
>>> list.take(2)
[a, b]
>>> list.take(10)
[a, b, c]
>>> list.take(0)
[]

takeWhile(predicate: (T) -> Boolean): List<T> 挑出滿(mǎn)足條件的元素的子集合

函數(shù)定義:

public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> {
    val list = ArrayList<T>()
    for (item in this) {
        if (!predicate(item))
            break
        list.add(item)
    }
    return list
}

從第一個(gè)元素開(kāi)始,判斷是否滿(mǎn)足predicate為true,如果滿(mǎn)足條件的元素就丟到返回ArrayList中。只要遇到任何一個(gè)元素不滿(mǎn)足條件,就結(jié)束循環(huán),返回list 。

代碼示例:

>>> val list = listOf(1,2,4,6,8,9)
>>> list.takeWhile({it%2==0})
[]
>>> list.takeWhile({it%2==1})
[1]

>>> val list = listOf(2,4,6,8,9,11,12,16)
>>> list.takeWhile({it%2==0})
[2, 4, 6, 8]

takeLast 挑出后n個(gè)元素的子集合

函數(shù)定義:

public fun <T> List<T>.takeLast(n: Int): List<T> {
    require(n >= 0) { "Requested element count $n is less than zero." }
    if (n == 0) return emptyList()
    val size = size
    if (n >= size) return toList()
    if (n == 1) return listOf(last())
    val list = ArrayList<T>(n)
    if (this is RandomAccess) {
        for (index in size - n .. size - 1)
            list.add(this[index])
    } else {
        for (item in listIterator(n))
            list.add(item)
    }
    return list
}

從集合倒數(shù)n個(gè)元素起,取出到最后一個(gè)元素的子集合。如果傳入0,返回空集。如果傳入n大于集合size,返回整個(gè)集合。如果傳入負(fù)數(shù),直接拋出IllegalArgumentException。

代碼示例:

>>> val list = listOf(2,4,6,8,9,11,12,16)
>>> list.takeLast(0)
[]
>>> list.takeLast(3)
[11, 12, 16]
>>> list.takeLast(100)
[2, 4, 6, 8, 9, 11, 12, 16]
>>> list.takeLast(-1)
java.lang.IllegalArgumentException: Requested element count -1 is less than zero.
    at kotlin.collections.CollectionsKt___CollectionsKt.takeLast(_Collections.kt:734)

takeLastWhile(predicate: (T) -> Boolean) 從最后開(kāi)始挑出滿(mǎn)足條件元素的子集合

函數(shù)定義:

public inline fun <T> List<T>.takeLastWhile(predicate: (T) -> Boolean): List<T> {
    if (isEmpty())
        return emptyList()
    val iterator = listIterator(size)
    while (iterator.hasPrevious()) {
        if (!predicate(iterator.previous())) {
            iterator.next()
            val expectedSize = size - iterator.nextIndex()
            if (expectedSize == 0) return emptyList()
            return ArrayList<T>(expectedSize).apply {
                while (iterator.hasNext())
                    add(iterator.next())
            }
        }
    }
    return toList()
}

反方向取滿(mǎn)足條件的元素,遇到不滿(mǎn)足的元素,直接終止循環(huán),并返回子集合。

代碼示例:

>>> val list = listOf(2,4,6,8,9,11,12,16)
>>> list.takeLastWhile({it%2==0})
[12, 16]

drop(n: Int) 去除前n個(gè)元素返回剩下的元素的子集合

函數(shù)定義:

public fun <T> Iterable<T>.drop(n: Int): List<T> {
    require(n >= 0) { "Requested element count $n is less than zero." }
    if (n == 0) return toList()
    val list: ArrayList<T>
    if (this is Collection<*>) {
        val resultSize = size - n
        if (resultSize <= 0)
            return emptyList()
        if (resultSize == 1)
            return listOf(last())
        list = ArrayList<T>(resultSize)
        if (this is List<T>) {
            if (this is RandomAccess) {
                for (index in n..size - 1)
                    list.add(this[index])
            } else {
                for (item in listIterator(n))
                    list.add(item)
            }
            return list
        }
    }
    else {
        list = ArrayList<T>()
    }
    var count = 0
    for (item in this) {
        if (count++ >= n) list.add(item)
    }
    return list.optimizeReadOnlyList()
}

代碼示例:

>>> val list = listOf(2,4,6,8,9,11,12,16)
>>> list.drop(5)
[11, 12, 16]
>>> list.drop(100)
[]
>>> list.drop(0)
[2, 4, 6, 8, 9, 11, 12, 16]
>>> list.drop(-1)
java.lang.IllegalArgumentException: Requested element count -1 is less than zero.
    at kotlin.collections.CollectionsKt___CollectionsKt.drop(_Collections.kt:538)

dropWhile(predicate: (T) -> Boolean) 去除滿(mǎn)足條件的元素返回剩下的元素的子集合

函數(shù)定義:

public inline fun <T> Iterable<T>.dropWhile(predicate: (T) -> Boolean): List<T> {
    var yielding = false
    val list = ArrayList<T>()
    for (item in this)
        if (yielding)
            list.add(item)
        else if (!predicate(item)) {
            list.add(item)
            yielding = true
        }
    return list
}

去除滿(mǎn)足條件的元素,當(dāng)遇到一個(gè)不滿(mǎn)足條件的元素時(shí),中止操作,返回剩下的元素子集合。

代碼示例:

>>> val list = listOf(2,4,6,8,9,11,12,16)
>>> list.dropWhile({it%2==0})
[9, 11, 12, 16]

dropLast(n: Int) 從最后去除n個(gè)元素

函數(shù)定義:

public fun <T> List<T>.dropLast(n: Int): List<T> {
    require(n >= 0) { "Requested element count $n is less than zero." }
    return take((size - n).coerceAtLeast(0))
}

代碼示例:

>>> val list = listOf(2,4,6,8,9,11,12,16)
>>> list.dropLast(3)
[2, 4, 6, 8, 9]
>>> list.dropLast(100)
[]
>>> list.dropLast(0)
[2, 4, 6, 8, 9, 11, 12, 16]
>>> list.dropLast(-1)
java.lang.IllegalArgumentException: Requested element count -1 is less than zero.
    at kotlin.collections.CollectionsKt___CollectionsKt.dropLast(_Collections.kt:573)

dropLastWhile(predicate: (T) -> Boolean) 從最后滿(mǎn)足條件的元素

函數(shù)定義:

public inline fun <T> List<T>.dropLastWhile(predicate: (T) -> Boolean): List<T> {
    if (!isEmpty()) {
        val iterator = listIterator(size)
        while (iterator.hasPrevious()) {
            if (!predicate(iterator.previous())) {
                return take(iterator.nextIndex() + 1)
            }
        }
    }
    return emptyList()
}

代碼示例:

>>> val list = listOf(2,4,6,8,9,11,12,16)
>>> list.dropLastWhile({it%2==0})
[2, 4, 6, 8, 9, 11]

slice(indices: IntRange) 取開(kāi)始下標(biāo)至結(jié)束下標(biāo)元素子集合

函數(shù)定義:

public fun <T> List<T>.slice(indices: IntRange): List<T> {
    if (indices.isEmpty()) return listOf()
    return this.subList(indices.start, indices.endInclusive + 1).toList()
}

代碼示例:

val list = listOf(2,4,6,8,9,11,12,16)
>>> list
[2, 4, 6, 8, 9, 11, 12, 16]
>>> list.slice(1..3)
[4, 6, 8]
>>> list.slice(2..7)
[6, 8, 9, 11, 12, 16]
>>> list
[2, 4, 6, 8, 9, 11, 12, 16]
>>> list.slice(1..3)
[4, 6, 8]
>>> list.slice(2..7)
[6, 8, 9, 11, 12, 16]

slice(indices: Iterable<Int>)返回指定下標(biāo)的元素子集合

函數(shù)定義:

public fun <T> List<T>.slice(indices: Iterable<Int>): List<T> {
    val size = indices.collectionSizeOrDefault(10)
    if (size == 0) return emptyList()
    val list = ArrayList<T>(size)
    for (index in indices) {
        list.add(get(index))
    }
    return list
}

這個(gè)函數(shù)從簽名上看,不是那么簡(jiǎn)單直接。從函數(shù)的定義看,這里的indices是當(dāng)做原來(lái)集合的下標(biāo)來(lái)使用的。

代碼示例:

>>> list
[2, 4, 6, 8, 9, 11, 12, 16]
>>> list.slice(listOf(2,4,6))
[6, 9, 12]

我們可以看出,這里是取出下標(biāo)為2,4,6的元素。而不是直觀理解上的,去掉元素2,4,6。

filterTo(destination: C, predicate: (T) -> Boolean) 過(guò)濾出滿(mǎn)足條件的元素并賦值給destination

函數(shù)定義:

public inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destination: C, predicate: (T) -> Boolean): C {
    for (element in this) if (predicate(element)) destination.add(element)
    return destination
}

把滿(mǎn)足過(guò)濾條件的元素組成的子集合賦值給入?yún)estination。

代碼示例:

>>> val list = listOf(1,2,3,4,5,6,7)
>>> val dest = mutableListOf<Int>()
>>> list.filterTo(dest,{it>3})
[4, 5, 6, 7]
>>> dest
[4, 5, 6, 7]

filter(predicate: (T) -> Boolean)過(guò)濾出滿(mǎn)足條件的元素組成的子集合

函數(shù)定義:

public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
    return filterTo(ArrayList<T>(), predicate)
}

相對(duì)于filterTo函數(shù),filter函數(shù)更加簡(jiǎn)單易用。從源碼我們可以看出,filter函數(shù)直接調(diào)用的filterTo(ArrayList<T>(), predicate), 其中入?yún)estination被直接默認(rèn)賦值為ArrayList<T>()。

代碼示例:

>>> val list = listOf(1,2,3,4,5,6,7)
>>> list.filter({it>3})
[4, 5, 6, 7]

另外,還有下面常用的過(guò)濾函數(shù):

filterNot(predicate: (T) -> Boolean), 用來(lái)過(guò)濾所有不滿(mǎn)足條件的元素 ;
filterNotNull() 過(guò)濾掉null元素。

5.3.7 映射操作符

map(transform: (T) -> R): List<R>

將集合中的元素通過(guò)轉(zhuǎn)換函數(shù)transform映射后的結(jié)果,存到一個(gè)集合中返回。

>>> val list = listOf(1,2,3,4,5,6,7)
>>> list.map({it})
[1, 2, 3, 4, 5, 6, 7]
>>> list.map({it*it})
[1, 4, 9, 16, 25, 36, 49]
>>> list.map({it+10})
[11, 12, 13, 14, 15, 16, 17]

這個(gè)函數(shù)內(nèi)部調(diào)用的是

public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
    return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}

這里的mapTo函數(shù)定義如下:

public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C {
    for (item in this)
        destination.add(transform(item))
    return destination
}

我們可以看出,這個(gè)map實(shí)現(xiàn)的原理是循環(huán)遍歷原集合中的元素,并把通過(guò)transform映射后的結(jié)果放到一個(gè)新的destination集合中,并返回destination。

mapIndexed(transform: (kotlin.Int, T) -> R)

轉(zhuǎn)換函數(shù)transform中帶有下標(biāo)參數(shù)。也就是說(shuō)我們可以同時(shí)使用下標(biāo)和元素的值來(lái)進(jìn)行轉(zhuǎn)換。 其中,第一個(gè)參數(shù)是Int類(lèi)型的下標(biāo)。

代碼示例:

>>> val list = listOf(1,2,3,4,5,6,7)
>>> list.mapIndexed({index,it -> index*it})
[0, 2, 6, 12, 20, 30, 42]

mapNotNull(transform: (T) -> R?)

遍歷集合每個(gè)元素,得到通過(guò)函數(shù)算子transform映射之后的值,剔除掉這些值中的null,返回一個(gè)無(wú)null元素的集合。

代碼示例:

>>> val list = listOf("a","b",null,"x",null,"z")
>>> list.mapNotNull({it})
[a, b, x, z]

這個(gè)函數(shù)內(nèi)部實(shí)現(xiàn)是調(diào)用的mapNotNullTo函數(shù):

public inline fun <T, R : Any, C : MutableCollection<in R>> Iterable<T>.mapNotNullTo(destination: C, transform: (T) -> R?): C {
    forEach { element -> transform(element)?.let { destination.add(it) } }
    return destination
}

flatMap(transform: (T) -> Iterable<R>): List<R>

在原始集合的每個(gè)元素上調(diào)用transform轉(zhuǎn)換函數(shù),得到的映射結(jié)果組成的單個(gè)列表。為了更簡(jiǎn)單的理解這個(gè)函數(shù),我們跟map(transform: (T) -> R): List<R>對(duì)比下。

首先看函數(shù)的各自的實(shí)現(xiàn):

map:

public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
    return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}

public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C {
    for (item in this)
        destination.add(transform(item))
    return destination
}

flatMap:

public inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}

public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.flatMapTo(destination: C, transform: (T) -> Iterable<R>): C {
    for (element in this) {
        val list = transform(element)
        destination.addAll(list)
    }
    return destination
}

我們可以看出,這兩個(gè)函數(shù)主要區(qū)別在transform函數(shù)返回上。

代碼示例

>>> val list = listOf("a","b","c")
>>> list.map({it->listOf(it+1,it+2,it+3)})
[[a1, a2, a3], [b1, b2, b3], [c1, c2, c3]]
>>> list.flatMap({it->listOf(it+1,it+2,it+3)})
[a1, a2, a3, b1, b2, b3, c1, c2, c3]

從代碼運(yùn)行結(jié)果我們可以看出,使用 map 是把list中的每一個(gè)元素都映射成一個(gè)List-n,然后以這些List-n為元素,組成一個(gè)大的嵌套的List返回。而使用flatMap則是把list中的第一個(gè)元素映射成一個(gè)List1,然后把第二個(gè)元素映射成的List2跟List1合并:List1.addAll(List2),以此類(lèi)推。最終返回一個(gè)“扁平的”(flat)List。

其實(shí),這個(gè)flatMap的過(guò)程是 map + flatten兩個(gè)操作的組合。這個(gè)flatten函數(shù)定義如下:

public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
    val result = ArrayList<T>()
    for (element in this) {
        result.addAll(element)
    }
    return result
}

代碼示例:

>>> val list = listOf("a","b","c")
>>> list.map({it->listOf(it+1,it+2,it+3)})
[[a1, a2, a3], [b1, b2, b3], [c1, c2, c3]]
>>> list.map({it->listOf(it+1,it+2,it+3)}).flatten()
[a1, a2, a3, b1, b2, b3, c1, c2, c3]

5.3.8 分組操作符

groupBy(keySelector: (T) -> K): Map<K, List<T>>

將集合中的元素按照條件選擇器keySelector(是一個(gè)函數(shù))分組,并返回Map。
代碼示例:

>>> val words = listOf("a", "abc", "ab", "def", "abcd")
>>> val lengthGroup = words.groupBy { it.length }
>>> lengthGroup
{1=[a], 3=[abc, def], 2=[ab], 4=[abcd]}

groupBy(keySelector: (T) -> K, valueTransform: (T) -> V)

分組函數(shù)還有一個(gè)是groupBy(keySelector: (T) -> K, valueTransform: (T) -> V),根據(jù)條件選擇器keySelector和轉(zhuǎn)換函數(shù)valueTransform分組。

代碼示例

>>> val programmer = listOf("K&R" to "C", "Bjar" to "C++", "Linus" to "C", "James" to "Java")
>>> programmer
[(K&R, C), (Bjar, C++), (Linus, C), (James, Java)]
>>> programmer.groupBy({it.second}, {it.first})
{C=[K&R, Linus], C++=[Bjar], Java=[James]}


這里涉及到一個(gè) 二元組Pair 類(lèi),該類(lèi)是Kotlin提供的用來(lái)處理二元數(shù)據(jù)組的。 可以理解成Map中的一個(gè)鍵值對(duì),比如Pair(“key”,”value”) 等價(jià)于 “key” to “value”。

我們?cè)偻ㄟ^(guò)下面的代碼示例,來(lái)看一下這兩個(gè)分組的區(qū)別:

>>> val words = listOf("a", "abc", "ab", "def", "abcd")
>>> words.groupBy( { it.length })
{1=[a], 3=[abc, def], 2=[ab], 4=[abcd]}
>>> words.groupBy( { it.length },{it.contains("b")})
{1=[false], 3=[true, false], 2=[true], 4=[true]}

我們可以看出,后者是在前者的基礎(chǔ)上又映射了一次{it.contains("b")},把第2次映射的結(jié)果放到返回的Map中了。

groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K>

另外,我們還可以使用groupingBy(crossinline keySelector: (T) -> K): Grouping<T, K>函數(shù)來(lái)創(chuàng)建一個(gè)Grouping,然后調(diào)用計(jì)數(shù)函數(shù)eachCount統(tǒng)計(jì)分組:

代碼示例

>>> val words = "one two three four five six seven eight nine ten".split(' ')
>>> words.groupingBy({it.first()}).eachCount()
{o=1, t=3, f=2, s=2, e=1, n=1}

上面的例子是統(tǒng)計(jì)words列表的元素單詞中首字母出現(xiàn)的頻數(shù)。

其中,eachCount函數(shù)定義如下:

@SinceKotlin("1.1")
@JvmVersion
public fun <T, K> Grouping<T, K>.eachCount(): Map<K, Int> =
        // fold(0) { acc, e -> acc + 1 } optimized for boxing
        foldTo( destination = mutableMapOf(),
                initialValueSelector = { _, _ -> kotlin.jvm.internal.Ref.IntRef() },
                operation = { _, acc, _ -> acc.apply { element += 1 } })
        .mapValuesInPlace { it.value.element }

5.3.9 排序操作符

reversed(): List<T>

倒序排列集合元素。
代碼示例

>>> val list = listOf(1,2,3)
>>> list.reversed()
[3, 2, 1]

這個(gè)函數(shù),Kotlin是直接調(diào)用的java.util.Collections.reverse()方法。其相關(guān)代碼如下:

public fun <T> Iterable<T>.reversed(): List<T> {
    if (this is Collection && size <= 1) return toList()
    val list = toMutableList()
    list.reverse()
    return list
}

public fun <T> MutableList<T>.reverse(): Unit {
    java.util.Collections.reverse(this)
}


sortedsortedDescending

升序排序和降序排序。

代碼示例

>>> val list = listOf(1,3,2)
>>> list.sorted()
[1, 2, 3]
>>> list.sortedDescending()
[3, 2, 1]

sortedBysortedByDescending

可變集合MutableList的排序操作。根據(jù)函數(shù)映射的結(jié)果進(jìn)行升序排序和降序排序。
這兩個(gè)函數(shù)定義如下:

public inline fun <T, R : Comparable<R>> MutableList<T>.sortBy(crossinline selector: (T) -> R?): Unit {
    if (size > 1) sortWith(compareBy(selector))
}
public inline fun <T, R : Comparable<R>> MutableList<T>.sortByDescending(crossinline selector: (T) -> R?): Unit {
    if (size > 1) sortWith(compareByDescending(selector))
}

代碼示例

>>> val mlist = mutableListOf("abc","c","bn","opqde","")
>>> mlist.sortBy({it.length})
>>> mlist
[, c, bn, abc, opqde]
>>> mlist.sortByDescending({it.length})
>>> mlist
[opqde, abc, bn, c, ]

5.3.10 生產(chǎn)操作符

zip(other: Iterable<R>): List<Pair<T, R>>

兩個(gè)集合按照下標(biāo)配對(duì),組合成的每個(gè)Pair作為新的List集合中的元素,并返回。

如果兩個(gè)集合長(zhǎng)度不一樣,取短的長(zhǎng)度。

代碼示例

>>> val list1 = listOf(1,2,3)
>>> val list2 = listOf(4,5,6,7)
>>> val list3 = listOf("x","y","z")
>>> list1.zip(list3)
[(1, x), (2, y), (3, z)]
>>> list3.zip(list1)
[(x, 1), (y, 2), (z, 3)]
>>> list2.zip(list3)
[(4, x), (5, y), (6, z)]  // 取短的長(zhǎng)度
>>> list3.zip(list2)
[(x, 4), (y, 5), (z, 6)]
>>> list1.zip(listOf<Int>())
[]

這個(gè)zip函數(shù)的定義如下:

public infix fun <T, R> Iterable<T>.zip(other: Iterable<R>): List<Pair<T, R>> {
    return zip(other) { t1, t2 -> t1 to t2 }
}

我們可以看出,其內(nèi)部是調(diào)用了zip(other) { t1, t2 -> t1 to t2 }。這個(gè)函數(shù)定義如下:

public inline fun <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform: (a: T, b: R) -> V): List<V> {
    val first = iterator()
    val second = other.iterator()
    val list = ArrayList<V>(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10)))
    while (first.hasNext() && second.hasNext()) {
        list.add(transform(first.next(), second.next()))
    }
    return list
}

依次取兩個(gè)集合相同索引的元素,使用提供的轉(zhuǎn)換函數(shù)transform得到映射之后的值,作為元素組成一個(gè)新的List,并返回該List。列表的長(zhǎng)度取兩個(gè)集合中最短的。

代碼示例

>>> val list1 = listOf(1,2,3)
>>> val list2 = listOf(4,5,6,7)
>>> val list3 = listOf("x","y","z")
>>> list1.zip(list3, {t1,t2 -> t2+t1})
[x1, y2, z3]
>>> list1.zip(list2, {t1,t2 -> t1*t2})
[4, 10, 18]

unzip(): Pair<List<T>, List<R>>

首先這個(gè)函數(shù)作用在元素是Pair的集合類(lèi)上。依次取各個(gè)Pair元素的first, second值,分別放到List<T>、List<R>中,然后返回一個(gè)first為L(zhǎng)ist<T>,second為L(zhǎng)ist<R>的大的Pair。

函數(shù)定義

public fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
    val expectedSize = collectionSizeOrDefault(10)
    val listT = ArrayList<T>(expectedSize)
    val listR = ArrayList<R>(expectedSize)
    for (pair in this) {
        listT.add(pair.first)
        listR.add(pair.second)
    }
    return listT to listR
}

看到這里,仍然有點(diǎn)抽象,我們直接看代碼示例:

>>> val listPair = listOf(Pair(1,2),Pair(3,4),Pair(5,6))
>>> listPair
[(1, 2), (3, 4), (5, 6)]
>>> listPair.unzip()
([1, 3, 5], [2, 4, 6])

partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>>

根據(jù)判斷條件是否成立,將集合拆分成兩個(gè)子集合組成的 Pair。我們可以直接看函數(shù)的定義來(lái)更加清晰的理解這個(gè)函數(shù)的功能:

public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
    val first = ArrayList<T>()
    val second = ArrayList<T>()
    for (element in this) {
        if (predicate(element)) {
            first.add(element)
        } else {
            second.add(element)
        }
    }
    return Pair(first, second)
}

我們可以看出,這是一個(gè)內(nèi)聯(lián)函數(shù)。

代碼示例

>>> val list = listOf(1,2,3,4,5,6,7,8,9)
>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list.partition({it>5})
([6, 7, 8, 9], [1, 2, 3, 4, 5])

plus(elements: Iterable<T>): List<T>

合并兩個(gè)List。

函數(shù)定義

public operator fun <T> Iterable<T>.plus(elements: Iterable<T>): List<T> {
    if (this is Collection) return this.plus(elements)
    val result = ArrayList<T>()
    result.addAll(this)
    result.addAll(elements)
    return result
}

我們可以看出,這是一個(gè)操作符函數(shù)??梢杂谩?”替代 。

代碼示例

>>> val list1 = listOf(1,2,3)
>>> val list2 = listOf(4,5)
>>> list1.plus(list2)
[1, 2, 3, 4, 5]
>>> list1+list2
[1, 2, 3, 4, 5]

關(guān)于plus函數(shù)還有以下的重載函數(shù):

plus(element: T): List<T>
plus(elements: Array<out T>): List<T>
plus(elements: Sequence<T>): List<T>

等。

plusElement(element: T): List<T>

在集合中添加一個(gè)元素。
函數(shù)定義

@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.plusElement(element: T): List<T> {
    return plus(element)
}

我們可以看出,這個(gè)函數(shù)內(nèi)部是直接調(diào)用的plus(element: T): List<T>

代碼示例

>>> list1 + 10
[1, 2, 3, 10]
>>> list1.plusElement(10)
[1, 2, 3, 10]
>>> list1.plus(10)
[1, 2, 3, 10]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容