Skip to content

集合

在程式設計中,將資料分組為結構以便後續處理會非常有用。Kotlin 正是為此目的提供了集合。

Kotlin 提供了以下集合用於分組項目:

集合類型描述
清單項目的有序集合
集合項目的唯一無序集合
映射鍵值對的集合,其中鍵是唯一的且僅對應一個值

每種集合類型都可以是可變的或唯讀的。

清單

清單以項目新增的順序儲存項目,並允許重複的項目。

要建立唯讀清單 (List),請使用 listOf() 函式。

要建立可變清單 (MutableList),請使用 mutableListOf() 函式。

建立清單時,Kotlin 可以推斷儲存的項目類型。若要明確宣告類型,請在清單宣告後使用角括號 <> 新增類型:

kotlin
fun main() { 
    // 唯讀清單
    val readOnlyShapes = listOf("triangle", "square", "circle")
    println(readOnlyShapes)
    // [triangle, square, circle]
    
    // 具有明確類型宣告的可變清單
    val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
    println(shapes)
    // [triangle, square, circle]
}

為了防止不必要的修改,您可以將可變清單指派給 List,以建立其唯讀視圖:

kotlin
    val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
    val shapesLocked: List<String> = shapes

這也稱為 型別轉換

清單是有序的,因此要存取清單中的項目,請使用 索引存取運算子 []

kotlin
fun main() { 
    val readOnlyShapes = listOf("triangle", "square", "circle")
    println("清單中的第一個項目是:${readOnlyShapes[0]}")
    // The first item in the list is: triangle
}

要取得清單中的第一個或最後一個項目,請分別使用 .first().last() 函式:

kotlin
fun main() { 
    val readOnlyShapes = listOf("triangle", "square", "circle")
    println("清單中的第一個項目是:${readOnlyShapes.first()}")
    // The first item in the list is: triangle
}

.first().last() 函式是擴充函式的範例。若要呼叫物件上的擴充函式,請在物件名稱後方加上句點 .,然後寫上函式名稱。

擴充函式會在中級導覽中詳細介紹。 目前,您只需要知道如何呼叫它們。

要取得清單中的項目數量,請使用 .count() 函式:

kotlin
fun main() { 
    val readOnlyShapes = listOf("triangle", "square", "circle")
    println("此清單有 ${readOnlyShapes.count()} 個項目")
    // This list has 3 items
}

要檢查項目是否在清單中,請使用 in 運算子

kotlin
fun main() {
    val readOnlyShapes = listOf("triangle", "square", "circle")
    println("circle" in readOnlyShapes)
    // true
}

要從可變清單中新增或移除項目,請分別使用 .add().remove() 函式:

kotlin
fun main() { 
    val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
    // 將 "pentagon" 新增到清單
    shapes.add("pentagon") 
    println(shapes)  
    // [triangle, square, circle, pentagon]

    // 從清單中移除第一個 "pentagon"
    shapes.remove("pentagon") 
    println(shapes)  
    // [triangle, square, circle]
}

集合

清單是有序且允許重複項目,而集合則是無序且只儲存唯一項目。

要建立唯讀集合 (Set),請使用 setOf() 函式。

要建立可變集合 (MutableSet),請使用 mutableSetOf() 函式。

建立集合時,Kotlin 可以推斷儲存的項目類型。若要明確宣告類型,請在集合宣告後使用角括號 <> 新增類型:

kotlin
fun main() {
    // 唯讀集合
    val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
    // 具有明確類型宣告的可變集合
    val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
    
    println(readOnlyFruit)
    // [apple, banana, cherry]
}

您可以看到在先前的範例中,由於集合只包含唯一元素,重複的 "cherry" 項目被丟棄了。

為了防止不必要的修改,您可以將可變集合指派給 Set,以建立其唯讀視圖:

kotlin
    val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
    val fruitLocked: Set<String> = fruit

由於集合是無序的,您無法透過特定索引存取項目。

要取得集合中的項目數量,請使用 .count() 函式:

kotlin
fun main() { 
    val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
    println("此集合有 ${readOnlyFruit.count()} 個項目")
    // This set has 3 items
}

要檢查項目是否在集合中,請使用 in 運算子

kotlin
fun main() {
    val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
    println("banana" in readOnlyFruit)
    // true
}

要從可變集合中新增或移除項目,請分別使用 .add().remove() 函式:

kotlin
fun main() { 
    val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
    fruit.add("dragonfruit")    // 將 "dragonfruit" 新增到集合
    println(fruit)              // [apple, banana, cherry, dragonfruit]
    
    fruit.remove("dragonfruit") // 從集合中移除 "dragonfruit"
    println(fruit)              // [apple, banana, cherry]
}

映射

映射將項目儲存為鍵值對。您可以透過引用鍵來存取值。您可以將映射想像成一份食物菜單。您可以透過找到您想吃的食物 (鍵) 來找到價格 (值)。如果您想在不使用編號索引的情況下查詢值 (例如在清單中),映射會很有用。

  • 映射中的每個鍵都必須是唯一的,這樣 Kotlin 才能理解您想取得哪個值。
  • 映射中可以有重複的值。

要建立唯讀映射 (Map),請使用 mapOf() 函式。

要建立可變映射 (MutableMap),請使用 mutableMapOf() 函式。

建立映射時,Kotlin 可以推斷儲存的項目類型。若要明確宣告類型,請在映射宣告後使用角括號 <> 新增鍵和值的類型。例如:MutableMap<String, Int>。鍵的類型是 String,值的類型是 Int

建立映射最簡單的方式是在每個鍵及其相關值之間使用 to

kotlin
fun main() {
    // 唯讀映射
    val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println(readOnlyJuiceMenu)
    // {apple=100, kiwi=190, orange=100}

    // 具有明確類型宣告的可變映射
    val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println(juiceMenu)
    // {apple=100, kiwi=190, orange=100}
}

為了防止不必要的修改,您可以將可變映射指派給 Map,以建立其唯讀視圖:

kotlin
    val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    val juiceMenuLocked: Map<String, Int> = juiceMenu

要存取映射中的值,請使用 索引存取運算子 [] 及其鍵:

kotlin
fun main() {
    // 唯讀映射
    val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println("蘋果汁的值是:${readOnlyJuiceMenu["apple"]}")
    // The value of apple juice is: 100
}

如果您嘗試使用映射中不存在的鍵存取鍵值對,您會看到 null 值:

kotlin
fun main() {
    // 唯讀映射
    val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println("鳳梨汁的值是:${readOnlyJuiceMenu["pineapple"]}")
    // The value of pineapple juice is: null
}

本導覽會在稍後的空值安全章節中解釋空值。

您也可以使用 索引存取運算子 [] 向可變映射新增項目:

kotlin
fun main() {
    val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    juiceMenu["coconut"] = 150 // 將鍵 "coconut" 及值 150 新增到映射
    println(juiceMenu)
    // {apple=100, kiwi=190, orange=100, coconut=150}
}

要從可變映射中移除項目,請使用 .remove() 函式:

kotlin
fun main() {
    val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    juiceMenu.remove("orange")    // 從映射中移除鍵 "orange"
    println(juiceMenu)
    // {apple=100, kiwi=190}
}

要取得映射中的項目數量,請使用 .count() 函式:

kotlin
fun main() {
    // 唯讀映射
    val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println("此映射有 ${readOnlyJuiceMenu.count()} 個鍵值對")
    // This map has 3 key-value pairs
}

要檢查映射中是否已包含特定鍵,請使用 .containsKey() 函式:

kotlin
fun main() {
    val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println(readOnlyJuiceMenu.containsKey("kiwi"))
    // true
}

要取得映射的鍵或值的集合,請分別使用 keysvalues 屬性:

kotlin
fun main() {
    val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println(readOnlyJuiceMenu.keys)
    // [apple, kiwi, orange]
    println(readOnlyJuiceMenu.values)
    // [100, 190, 100]
}

keysvalues 是物件屬性的範例。要存取物件的屬性,請在物件名稱後方加上句點 .,然後寫上屬性名稱。

屬性會在類別章節中更詳細地討論。 在本導覽的這一點,您只需要知道如何存取它們。

要檢查鍵或值是否在映射中,請使用 in 運算子

kotlin
fun main() {
    val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
    println("orange" in readOnlyJuiceMenu.keys)
    // true
    
    // 或者,您不需要使用 keys 屬性
    println("orange" in readOnlyJuiceMenu)
    // true
    
    println(200 in readOnlyJuiceMenu.values)
    // false
}

有關集合功能的更多資訊,請參閱集合

既然您已經了解基本類型以及如何管理集合,現在是時候探索您可以在程式中使用的控制流程了。

練習

練習 1

您有一個「綠色」數字清單和一個「紅色」數字清單。完成程式碼以列印總共有多少個數字。

kotlin
fun main() {
    val greenNumbers = listOf(1, 4, 23)
    val redNumbers = listOf(17, 2)
    // 在此撰寫您的程式碼
}
範例解答
kotlin
fun main() {
    val greenNumbers = listOf(1, 4, 23)
    val redNumbers = listOf(17, 2)
    val totalCount = greenNumbers.count() + redNumbers.count()
    println(totalCount)
}
練習 2

您有一個伺服器支援的通訊協定集合。使用者請求使用特定通訊協定。完成程式以檢查請求的通訊協定是否受支援 (isSupported 必須是布林值)。

kotlin
fun main() {
    val SUPPORTED = setOf("HTTP", "HTTPS", "FTP")
    val requested = "smtp"
    val isSupported = // 在此撰寫您的程式碼 
    println("Support for $requested: $isSupported")
}
範例解答
kotlin
fun main() {
    val SUPPORTED = setOf("HTTP", "HTTPS", "FTP")
    val requested = "smtp"
    val isSupported = requested.uppercase() in SUPPORTED
    println("Support for $requested: $isSupported")
}
練習 3

定義一個映射,將整數數字 1 到 3 與其對應的拼寫關聯起來。使用此映射來拼寫給定的數字。

kotlin
fun main() {
    val number2word = // 在此撰寫您的程式碼
    val n = 2
    println("$n is spelt as '${<在此撰寫您的程式碼 >}'")
}
範例解答
kotlin
fun main() {
    val number2word = mapOf(1 to "one", 2 to "two", 3 to "three")
    val n = 2
    println("$n is spelt as '${number2word[n]}'")
}