Kotlin集合—MutableList可变列表、Set、MuTableSet

MutableList可变列表:

package com.init.demo.collection

fun main(args: Array<String>) {
    /**
     * MutableList可变列表
     * 定义:MutableList<类型>或mutableListOf(元素1,元素2,....,元素n)
     * 可以改变自身大小的数组
     */
    val nollNewsStations = mutableListOf("北京","上海","广东","杭州","天津","四川","重庆","湖北","广西","河南","河北")
    //"往末尾添加新元素add:"
    nollNewsStations.add("新疆")
    println(nollNewsStations)
    //可以添加另一个List、Array、Set等只要看起来是序列的 addAll
    val newsStations = mutableListOf("西藏","湖南")
    nollNewsStations.addAll(newsStations)
    println(nollNewsStations)
    //移除指定位置的元素 removeAt
    nollNewsStations.removeAt(4)
    println(nollNewsStations)
    //移除一个元素 remove
    nollNewsStations.remove("广东")
    println(nollNewsStations)
    //替换指定位置的元素 set或下标方法
    nollNewsStations.set(2,"山西")
    nollNewsStations[4] = "陕西"
    println(nollNewsStations)
    //取子列表类似Array的sliceArray方法 subList
    val subStation =nollNewsStations.subList(1,5)
    println(subStation)
    //丢弃系列:drop dropLast  dropLastWhile dropWhile 用法可以参照数组
    //特别系列:drop不同于remove方法 drop系列不会对MutableList本身可变列表产生任何影响
    //清空; clear removeAll

    nollNewsStations.clear()
    println(nollNewsStations)
    nollNewsStations.removeAll(newsStations)
    println(nollNewsStations)

    /**
     * Map 的特性是无序可以重复  但有一个不重复的key
     *  由键值对组成 属性keys (是一个set)、values
     *  解决辨识重复元素
     */

    //输出:
   /* [北京, 上海, 广东, 杭州, 天津, 四川, 重庆, 湖北, 广西, 河南, 河北, 新疆]
    [北京, 上海, 广东, 杭州, 天津, 四川, 重庆, 湖北, 广西, 河南, 河北, 新疆, 西藏, 湖南]
    [北京, 上海, 广东, 杭州, 四川, 重庆, 湖北, 广西, 河南, 河北, 新疆, 西藏, 湖南]
    [北京, 上海, 杭州, 四川, 重庆, 湖北, 广西, 河南, 河北, 新疆, 西藏, 湖南]
    [北京, 上海, 山西, 四川, 陕西, 湖北, 广西, 河南, 河北, 新疆, 西藏, 湖南]
    [上海, 山西, 四川, 陕西]
    []
    []*/
}

Set应用:

package com.init.demo.collection

fun main(args: Array<String>) {
    /**
     * set Set<类型>或setOf(元素1,元素2,....,元素n)
     * 大小固定  元素类型不可变
     * 常规方法 计数 size count  空否 isEmpty
     * 包含某个元素 contains
     * 转换数组 toTypedArray
     */

    val city1 = setOf("阜南","贵州","湖北","重庆")
    val city2 = setOf("重庆","四川","云南")
    val city3 = setOf("重庆","陕西","甘肃")

    /**
     * 子集和父集
     * 如果集合A中所有的元素在另一个集合B中,那么可以称A是B的子集
     * 可以说是B包含A A包含于B
     */

    println(city1.containsAll(city2))
    /**
     * 两个Set可以用+组合成一个Set
     */
    println((city1+city2).containsAll(city2))
    println((city1+city2).containsAll(city1))

    /******************************************************两个集合之间的运算:交集、差集、并集、补集*********************************************/
    //交集 两个集合都包含的元素 intersect
    println(city1.intersect(city2))
    println(city1.intersect(city3))
    println(city2.intersect(city3))
    //差集 从一个集合排除与另一个集合交集的部分 subtract
    println(city1.subtract(city2))
    println(city1.subtract(city3))
    println(city2.subtract(city3))
    //并集 2个集合合并成一个集合 union
    println(city1.union(city2).union(city3))
    //补集 并集去除交集的部分 union
    /**
     * set中并没有补集的方法 但可以用运算符进行运算
     */
    println(city1.union(city2)-city1.intersect(city2))
    println(city1.union(city3)-city1.intersect(city3))
    //输出
    /*false
    true
    true
    [重庆]
    [重庆]
    [重庆]
    [阜南, 贵州, 湖北]
    [阜南, 贵州, 湖北]
    [四川, 云南]
    [阜南, 贵州, 湖北, 重庆, 四川, 云南, 陕西, 甘肃]
    [阜南, 贵州, 湖北, 四川, 云南]
    [阜南, 贵州, 湖北, 陕西, 甘肃]*/
}

MuTableSet应用:

package com.init.demo.collection

fun main(args: Array<String>) {
    /**
     * MuTableSet 与MutableList相似
     * 定义:MuTableSet<类型>或mutableSetOf(元素1,元素2,....,元素n)
     */

    val city1 = setOf("阜南","贵州","湖北","重庆")
    val city2 = setOf("重庆","四川","云南")
    val city3 = setOf("重庆","陕西","甘肃")

    //set换换成MuTableSet toMutableSet
    var  city = city1.union(city2).union(city3).toMutableSet()

    //往末尾添加元素
    city.add("北京")
    println(city)
    //添加另一个集合
    val newsStations = mutableListOf("西藏","湖南")
    city.addAll(newsStations)
    println(city)
    //移除一个元素
    city.remove("西藏")
    println(city)
    //移除另一个集合
    city.removeAll(newsStations)
    println(city)

    //输出:
    /*[阜南, 贵州, 湖北, 重庆, 四川, 云南, 陕西, 甘肃, 北京]
    [阜南, 贵州, 湖北, 重庆, 四川, 云南, 陕西, 甘肃, 北京, 西藏, 湖南]
    [阜南, 贵州, 湖北, 重庆, 四川, 云南, 陕西, 甘肃, 北京, 湖南]
    [阜南, 贵州, 湖北, 重庆, 四川, 云南, 陕西, 甘肃, 北京]*/

}

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值