Android Kotlin集合常用操作

Android Kotlin集合常用操作

最近也是学了很多kotlin的知识,然后也整理了一些集合相关的一些常用的操作,然后选了一些列举一下。

直接上代码!!!!!!!

val lists: MutableList<Student> = ArrayList()
        lists.add(Student("洒家买蘑菇", 15, "哈哈哈"))
        lists.add(Student("王路飞", 17, "嘻嘻嘻"))
        lists.add(Student("刘索隆", 18, "呵呵呵"))
        lists.add(Student("克山治", 18, "嘿嘿嘿"))
        lists.add(Student("猫娜美", 16, "咯咯咯"))

 /**
         * 找出名字等于 王路飞的 返回的是对象
         */
        lists.find {
            it.name == "王路飞"
        }?.let {
            Log.e("testList", it.toString())
            //打印结果:Student(name='王路飞', age=17, className='嘻嘻嘻', url='')
        }

        /**
         * 找出年龄等于18的返回的是一个数组
         */
        lists.filter {
            it.age == 18
        }.let {
            Log.e("testList", it.toString())
            //打印结果:[Student(name='刘索隆', age=18, className='呵呵呵', url=''), Student(name='克山治', age=18, className='嘿嘿嘿', url='')]
        }

        /**
         * 只有当所有元素都满足条件的时候才返回 true。
         */
        val all: Boolean = lists.all {
            it.age >= 10
        }
        Log.e("testList", "$all")
        //打印结果:true


        /**
         * 如果有任何元素满足条件就返回 true
         */
        val any:Boolean = lists.any {
            it.age == 16
        }
        Log.e("testList", "$any")
        //打印结果:true


        /**
         * 遍历
         */
        lists.forEach {
            Log.e("test",it.toString())
            //打印结果:
            //Student(name='洒家买蘑菇', age=15, className='哈哈哈', url='')
            //Student(name='王路飞', age=17, className='嘻嘻嘻', url='')
            //Student(name='刘索隆', age=18, className='呵呵呵', url='')
            //Student(name='克山治', age=18, className='嘿嘿嘿', url='')
            //Student(name='猫娜美', age=16, className='咯咯咯', url='')
        }


        /**
         * 返回包含去掉前n个元素的所有元素的列表。
         */
        lists.drop(1)
        Log.e("testList2222",lists.drop(3).toString())
        //打印结果: [Student(name='克山治', age=18, className='嘿嘿嘿', url=''), Student(name='猫娜美', age=16, className='咯咯咯', url='')]


        /**
         * 返回集合的第一个元素这里要主要如果集合为空会抛出异常 java.util.NoSuchElementException: List is empty
         */
        lists.first()
        Log.e("testList22221111","${lists.first()}")
        //打印结果: Student(name='洒家买蘑菇', age=15, className='哈哈哈', url='')


        /**
         * 返回集合的第一个元素 并判断指定的条件  如果 条件不成立  就会 NoSuchElementException 异常
         */
        lists.first {
            it.age==10
        }
        //打印结果:java.util.NoSuchElementException: Collection contains no element matching the predicate.


        /**
         * 获取集合的第一个元素   没有就返回null
         */
        lists.firstOrNull()
        Log.e("testList22221111","${lists.firstOrNull()}")
        //打印结果: Student(name='洒家买蘑菇', age=15, className='哈哈哈', url='')


        /**
         * 获取集合的第一个元素   判断指定的条件  不成立就返回null
         */
       lists.firstOrNull {
            it.age==10
        }
        //打印结果:null

        /**
         * 获取集合的最后一个元素  于 lists.first()相反
         */
        lists.last()
        Log.e("werwerwer","${lists.last()}")
        //打印结果:Student(name='猫娜美', age=16, className='咯咯咯', url='')


        /**
         * 获取集合的最后一个元素  于 lists.first{...}相反
         */
        lists.last {
            it.age==10
        }

        /**
         * 与lists.firstOrNull()相反
         */
        lists.lastOrNull()


        /**
         * 与lists.firstOrNull{...}相反
         */
        lists.lastOrNull {
            it.age == 10
        }
 val intList:MutableList<Int> = ArrayList()
        intList.add(1)
        intList.add(2)
        intList.add(3)
        intList.add(4)
        intList.add(3)
        intList.add(4)
        intList.add(1)
/**
         * 计算出集合元素累加的结果。
         */
        intList.sum()
        Log.e("werwerwer333","${intList.sum()}")
        //打印结果  18


        /**
         * 获取平均数
         */
        intList.average()
        Log.e("werwerwer333","${intList.average()}")
        //打印结果:2.5714285714285716

        /**
         * 去掉重复的元素
         */
        intList.distinct()
        Log.e("werwerwer333","${intList.distinct()}")
        //打印结果:[1, 2, 3, 4]


        /**
         * 反序
         */
        intList.reversed()
        Log.e("werwerwer333","${intList.reversed()}")
        //打印结果:[1, 4, 3, 4, 3, 2, 1]


        /**
         * 自然升序
         */
        intList.sorted()
        Log.e("werwerwer333","${intList.sorted()}")
        //打印结果:[1, 1, 2, 3, 3, 4, 4]


        /**
         * 自然降序。
         */
        intList.sortedDescending()
        Log.e("werwerwer333","${intList.sortedDescending()}")
        //打印结果:[4, 4, 3, 3, 2, 1, 1]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值