Kotlin数据容器(3)✔️集合

2019-06-12  本文已影响0人  狼性代码人
  • 集合概述

  • Set 集合

    • 不可变 Set 集合 [setOf]
    • 可变 Set 集合 [mutableSetOf、hashSetOf]
  • List 集合

    • 不可变 List 集合 [listOf]
    • 可变 List 集合 [mutableListOf、arrayListOf]
  • Map 集合

    • 不可变 Map 集合 [mapOf]
    • 可变 Map 集合 [mutableMapOf、hashMapOf]

一、集合概述

  Kotlin 集合类型分为 CollectionMapMutableCollectionCollection 的可变的子接口,MutableMapMap 的可变子接口。Collection 还有两个重要的子接口 SetList,他们都有可变接口MutableSetMutableList

  提示:Kotlin 集合与Java 集合一个很大的不同是:Kotlin 将集合分为不可变集合和可变集合,以 Mutable 开头命名的接口都属于可变集合,可变集合包含了修改集合的函数 addremoveclear 等。

kotlin 主要集合接口和类

二、Set 集合

  Set 集合是由一串无序的、不能重复的相同类型元素构成的集合。Set 集合的接口分为不可变集合 kotlin.collections.Set 和可变集合kotlin.collections.MutableSet,以及 Java 提供的实现类 java.util.HashSet

1、不可变 Set 集合

  创建不可变 Set 集合可以使用工厂函数 setOf

  不可变 Set 集合接口是 kotlin.collections.Set,它也继承自 Collection 接口,kotlin.collections.Set 提供了一些集合操作函数和属性如下:

fun main(args: Array<String>?) {
    val set1 = setOf<Int>()
    val set2 = setOf("abc")
    val set3 = setOf(1, 2, 3, 4, 5)
    
    println(set1.isEmpty())
    println(set2.size)
    println(set3.contains(3))
    
    println("=== 1、使用for循环遍历 ===")
    for (item in set3) {
        println("读取集合元素:$item")
    }

    println("=== 2、使用迭代器循环遍历 ===")
    val iterator = set3.iterator()
    while (iterator.hasNext()) {
        val value = iterator.next()
        println("读取集合元素:$value")
    }
}

2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: true
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 1
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: true
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: === 1、使用for循环遍历 ===
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:1
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:2
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:3
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:4
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:5
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: === 2、使用迭代器循环遍历 ===
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:1
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:2
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:3
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:4
2019-06-12 16:00:59.527 9837-9837/cn.ak.kot I/System.out: 读取集合元素:5

2、可变 Set 集合

  创建可变 Set 合集可以使用工厂函数 mutableSetOfhashSetOf 等,mutableSetOf 创建的集合是 MutableSet 接口类型,而 hashSetOf 创建的集合是 HashSet 具体类型。每个函数都有两个版本:

  可变 Set 集合接口是 kotlin.collections.MutableSet,它也继承自 kotlin.collections.Set 接口,kotlin.collections.MutableSet 提供了一些修改集合的函数如下:

fun main(args: Array<String>?) {
    val mutableSet1 = mutableSetOf<String>()
    val mutableSet2 = mutableSetOf(1, 3, 4, 5, 79, 0, 2)
    mutableSet1.add("a")
    mutableSet1.add("b")
    mutableSet1.add("c")
    mutableSet1.add("b")    // 添加了重复元素,无效,元素个数还为3

    println("mutableSet1集合含有 ${mutableSet1.size} 个元素")
    println(mutableSet1)

    mutableSet1.remove("b")
    mutableSet1.remove("f")
    println("mutableSet1集合是否包含\"b\" -> ${mutableSet1.contains("b")}")

    mutableSet1.clear()
    println("mutableSet1集合是否为空集合 -> ${mutableSet1.isEmpty()}")



    val hashSet1 = hashSetOf<String>()
    val hashSet2 = hashSetOf(1, 3, 4, 5, 79, 0, 2)
    println("=== 1、使用for循环遍历 ===")
    for (item in hashSet2) {
        println("读取hashSet2集合元素:$item")
    }

    println("=== 2、使用迭代器循环遍历 ===")
    val iterator = hashSet2.iterator()
    while (iterator.hasNext()) {
        val value = iterator.next()
        println("读取hashSet2集合元素:$value")
    }
}

2019-06-12 16:26:53.644 10270-10270/cn.ak.kot I/System.out: mutableSet1集合含有 3 个元素
2019-06-12 16:26:53.644 10270-10270/cn.ak.kot I/System.out: [a, b, c]
2019-06-12 16:26:53.644 10270-10270/cn.ak.kot I/System.out: mutableSet1集合是否包含"b" -> false
2019-06-12 16:26:53.644 10270-10270/cn.ak.kot I/System.out: mutableSet1集合是否为空集合 -> true
2019-06-12 16:26:53.644 10270-10270/cn.ak.kot I/System.out: === 1、使用for循环遍历 ===
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:0
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:1
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:2
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:3
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:4
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:5
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:79
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: === 2、使用迭代器循环遍历 ===
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:0
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:1
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:2
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:3
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:4
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:5
2019-06-12 16:26:53.645 10270-10270/cn.ak.kot I/System.out: 读取hashSet2集合元素:79

三、List 集合

  List 集合中元素是有序的,可以重复出现。List 集合的接口分为不可变集合 kotlin.collections.List 和可变结合kotlin.collections.MutableList,以及 Java 提供的实现类 java.util.ArrayListjava.util.LinkedList

  提示:ListSet 集合相比,List 集合强调的是有序,Set 集合强调的是不重复。当不考虑顺序且没有重复元素时,ListSet 集合可以互相替换。

1、不可变 List 集合

  创建不可变 List 集合可以使用工厂函数 listOf

  不可变 List 集合接口是 kotlin.collections.List,它也继承自 Collection 接口,kotlin.collections.List 提供了一些集合操作函数和属性如下:

fun main(args: Array<String>?) {
    val list1 = listOf<Int>()
    val list2 = listOf("abc")
    val list3 = listOf(1, 2, 3, 4, 5)
    val list4 = list3.subList(1, 3)

    println(list1.isEmpty())
    println(list2.size)
    println(list3.contains(3))
    println(list3.indexOf(2))
    println(list4)

    println("=== 1、使用for循环遍历 ===")
    for (item in list3) {
        println("读取集合元素:$item")
    }

    println("=== 2、使用迭代器循环遍历 ===")
    val iterator = list3.iterator()
    while (iterator.hasNext()) {
        val value = iterator.next()
        println("读取集合元素:$value")
    }
}

2019-06-12 16:47:30.256 10611-10611/cn.ak.kot I/System.out: true
2019-06-12 16:47:30.256 10611-10611/cn.ak.kot I/System.out: 1
2019-06-12 16:47:30.256 10611-10611/cn.ak.kot I/System.out: true
2019-06-12 16:47:30.256 10611-10611/cn.ak.kot I/System.out: 1
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: [2, 3]
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: === 1、使用for循环遍历 ===
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:1
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:2
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:3
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:4
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:5
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: === 2、使用迭代器循环遍历 ===
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:1
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:2
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:3
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:4
2019-06-12 16:47:30.257 10611-10611/cn.ak.kot I/System.out: 读取集合元素:5

2、可变 List 集合

  创建可变 List 合集可以使用工厂函数 mutableListOfarrayListOf 等,mutableListOf 创建的集合是 MutableList 接口类型,而 arrayListOf 创建的集合是 ArrayList 具体类型。每个函数都有两个版本:

  可变 List 集合接口是 kotlin.collections.MutableList,它也继承自 kotlin.collections.List 接口,kotlin.collections.MutableList 提供了一些修改集合的操作函数如下:

fun main(args: Array<String>?) {
    val mutableList1 = mutableListOf<String>()
    val mutableList2 = mutableListOf(1, 3, 4, 5, 79, 0, 2)
    mutableList1.add("a")
    mutableList1.add("b")
    mutableList1.add("c")
    mutableList1.add("b")    // 可正常添加了重复元素,元素个数为4

    println("mutableList1集合含有 ${mutableList1.size} 个元素")
    println(mutableList1)

    mutableList1.remove("b")
    println(mutableList1)
    mutableList1.remove("f")
    println("mutableList1集合是否包含\"b\" -> ${mutableList1.contains("b")}")

    mutableList1.clear()
    println("mutableList1集合是否为空集合 -> ${mutableList1.isEmpty()}")



    val arrayList1 = arrayListOf<String>()
    val arrayList2 = arrayListOf(1, 3, 4, 5, 79, 0, 2)
    println("=== 1、使用for循环遍历 ===")
    for (item in arrayList2) {
        println("读取arrayList2集合元素:$item")
    }

    println("=== 2、使用迭代器循环遍历 ===")
    val iterator = arrayList2.iterator()
    while (iterator.hasNext()) {
        val value = iterator.next()
        println("读取arrayList2集合元素:$value")
    }
}

2019-06-12 17:04:23.930 11084-11084/cn.ak.kot I/System.out: mutableList1集合含有 4 个元素
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: [a, b, c, b]
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: [a, c, b]
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: mutableList1集合是否包含"b" -> true
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: mutableList1集合是否为空集合 -> true
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: === 1、使用for循环遍历 ===
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:1
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:3
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:4
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:5
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:79
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:0
2019-06-12 17:04:23.931 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:2
2019-06-12 17:04:23.932 11084-11084/cn.ak.kot I/System.out: === 2、使用迭代器循环遍历 ===
2019-06-12 17:04:23.932 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:1
2019-06-12 17:04:23.932 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:3
2019-06-12 17:04:23.932 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:4
2019-06-12 17:04:23.932 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:5
2019-06-12 17:04:23.932 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:79
2019-06-12 17:04:23.932 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:0
2019-06-12 17:04:23.933 11084-11084/cn.ak.kot I/System.out: 读取arrayList2集合元素:2

四、Map 集合

  Map 集合表示一种复杂的集合,允许按照某个键来访问元素。Map 集合是由两个集合构成,一个是键合集,一个是值集合。键集合是 Set 类型,因此不能有重复的元素。而值集合是 Collection 类型,可以有重复的集合。Map 集合中的键和值是成对出现的。Map 集合的接口分为不可变集合kotlin.collections.Map 和 可变集合 kotlin.collections.mutableMap,以及 Java 提供的实现类 java.util.HashMap

1、不可变 Map 集合

  创建不可变 Map 集合可以使用工厂函数 mapOf

  不可变 Map 集合接口是 kotlin.collections.Map,它也继承自 Collection 接口,kotlin.collections.Map 提供了一些集合操作函数和属性如下:

fun main(args: Array<String>?) {
    val map1 = mapOf<Int, String>()
    val map2 = mapOf(1 to 4)
    val map3 = mapOf(101 to "老大", 102 to "老二", 103 to "小三", 104 to "老二")

    println("集合 size = ${map3.size}")
    println(map3)
    println("103 - ${map3[103]}")
    println("键集合中是否包含 103 - ${map3.containsKey(103)}")
    println("值集合中是否包含 老二 - ${map3.containsValue("老二")}")
    println("值集合中是否包含 老五 - ${map3.containsValue("老五")}")

    println("判断map1集合是否为空 - ${map1.isEmpty()}")

    println("=== 1、使用for循环遍历 ===")
    val keys = map3.keys
    for (key in keys) {
        println("key=${key} - value=${map3[key]}")
    }

    println("=== 2、使用迭代器循环遍历 ===")
    val values = map3.values
    val iterator = values.iterator()
    while (iterator.hasNext()) {
        val value = iterator.next()
        println("值集合元素:$value")
    }
}

2019-06-12 17:45:57.719 11806-11806/cn.ak.kot I/System.out: 集合 size = 4
2019-06-12 17:45:57.719 11806-11806/cn.ak.kot I/System.out: {101=老大, 102=老二, 103=小三, 104=老二}
2019-06-12 17:45:57.719 11806-11806/cn.ak.kot I/System.out: 103 - 小三
2019-06-12 17:45:57.719 11806-11806/cn.ak.kot I/System.out: 键集合中是否包含 103 - true
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: 值集合中是否包含 老二 - true
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: 值集合中是否包含 老五 - false
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: 判断map1集合是否为空 - true
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: === 1、使用for循环遍历 ===
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: key=101 - value=老大
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: key=102 - value=老二
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: key=103 - value=小三
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: key=104 - value=老二
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: === 2、使用迭代器循环遍历 ===
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: 值集合元素:老大
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: 值集合元素:老二
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: 值集合元素:小三
2019-06-12 17:45:57.722 11806-11806/cn.ak.kot I/System.out: 值集合元素:老二

2、可变 Map 集合

  创建可变 Map 合集可以使用工厂函数 mutableMapOfhashMapOf 等,mutableMapOf 创建的集合是 MutableMap 接口类型,而 hashMapOf 创建的集合是 HashMap 具体类型。每个函数都有两个版本:

  可变 Map 集合接口是 kotlin.collections.MutableMap,它也继承自 kotlin.collections.Map 接口,kotlin.collections.MutableMap 提供了一些修改集合的操作函数如下:

fun main(args: Array<String>?) {
    val mutableMap1 = mutableMapOf<Int, String>()
    val mutableMap2 = mutableMapOf(101 to "老大")

    mutableMap2[102] = "老二"
    mutableMap2.put(103, "小三")
    println(mutableMap2)

    mutableMap2.put(103, "老三") // 键已经存在,则替换值
    println(mutableMap2)

    println("mutableMap1集合含有 ${mutableMap1.size} 个元素")
    println(mutableMap1)

    mutableMap2.remove(102)
    println(mutableMap2)

    mutableMap2.clear()
    println("mutableMap2集合是否为空集合 -> ${mutableMap2.isEmpty()}")
}

2019-06-12 18:02:43.316 12135-12135/cn.ak.kot I/System.out: {101=老大, 102=老二, 103=小三}
2019-06-12 18:02:43.316 12135-12135/cn.ak.kot I/System.out: {101=老大, 102=老二, 103=老三}
2019-06-12 18:02:43.316 12135-12135/cn.ak.kot I/System.out: mutableMap1集合含有 0 个元素
2019-06-12 18:02:43.316 12135-12135/cn.ak.kot I/System.out: {}
2019-06-12 18:02:43.316 12135-12135/cn.ak.kot I/System.out: {101=老大, 103=老三}
2019-06-12 18:02:43.316 12135-12135/cn.ak.kot I/System.out: mutableMap2集合是否为空集合 -> true

  提示:Map集合添加键值对时需要注意:如果键已经存在,则会替换原有值;如果这个键不存在,会添加一个键值对。

上一篇 下一篇

猜你喜欢

热点阅读