android compose kotlin collections的使用方法

collections包括List,MutableList,set ,map 等等。

1.List

    val users= listOf<User>(
        User(10,"Jone",80f),
        User(1,"alice",81f),
        User(2,"Bob",95f),
        User(3,"Peter",73f)
    )

    //筛选数据
    val sortedUsers = users.filter { it.id % 2 == 0 }
    println(sortedUsers)

    //累计数据
    val reduce = users.reduce {aa,bb ->User(math = aa.math+bb.math,id= 0, name = "")}
    println(reduce)
    //带初值累计数据,不可行?
    //val reduce = users.fold(User(math=1000f,id=0, name = "")) {aa,bb -> User(math=aa.math+bb.math,id=0, name = "") }

    //排序
    val sortedUser = users.sortedBy { it.id }
    println(sortedUser)
    //降序排序
    val sortedUser2 = users.sortedBy { -it.id }
    println(sortedUser2)

    //投影(投射)
    val names = users.map { it.name }
    println(names)

2.Map

2.1 遍历方式

    val numMap = mapOf("key1" to 1,"key2" to 2, "key3" to 3, "key4" to 4)

    println(numMap["key1"])

    val numMap2 = mapOf(1 to "key1",2 to "key2",3 to "key3",4 to "key4")
    println(numMap2[1])

    numMap2.forEach {
        key,value->
        println("$key->$value")
    }

    numMap2.forEach {
        entry->
        println(entry)
    }

2.2 map操作函数

    val numMap = mapOf("key1" to 1,"key2" to 2, "key3" to 3, "key4" to 4)
    println("${numMap.keys}  ${numMap.values}   ${numMap.entries}")

    val numMap2 = mapOf(1 to "key1",2 to "key22",3 to "key333",4 to "key44444")
    val filterMap2 = numMap2.filter { (k,v)-> k<3 && v.length<=5 }
    println(filterMap2)

    val filterMap3 = numMap2.filterKeys { it<=2 }
    val filterMap4 = numMap2.filterKeys { key-> key<=2 }
    println(filterMap4)

    val filterMap5 = numMap2.filterValues { it.endsWith("2")}
    println(filterMap5)


    val variableMap = mutableMapOf(1 to "key1",2 to "key2")
    println(variableMap)

    variableMap.put(1,"modify key1")
    println(variableMap)

    var out1 = variableMap.map { entry -> entry.key.toString()+"::" + entry.value+"sdf" }
    println(out1)


    val values = numMap.mapValues { it.value+1 }
    println(values)//{key1=2, key2=3, key3=4, key4=5}
    val keys = numMap.mapKeys { it.value+1 }
    println(keys)//{2=1, 3=2, 4=3, 5=4}
    

3.集合拷贝:toList()、toMutableList()

    val list1 = mutableListOf<User>()
    for(i in 0..2)
    {
        list1.add(User(id = i, name = "aaa", math = 80f))
    }
    println(list1)

    val copyList = list1.toMutableList()
    copyList.add(User(100,name="bbb", math = 90f))
    println(copyList)

    copyList.forEachIndexed { index,node -> node.name = "name$index" }
    println(copyList)

    copyList.forEach { node -> node.name="cccc" }
    println(copyList)

4.map(),zip(),associate(),flattern(),flatMap()

4.1 Map

    val numbers = listOf("one", "two", "three")
    val mIndexes = listOf(1, 2, 3)
    val mTwoIndex = listOf(1, 2)

    var map1 = (numbers.map { "it's $it" })
    println(map1)//[it's one, it's two, it's three]

    var mapnot1= numbers.mapNotNull { if (it.length == 3) null else it }
    println(mapnot1) //[three]


    var mapIndex1 = numbers.mapIndexed { index,s -> "$index-$s" }
    println(mapIndex1)

4.2 zip() 合拢

    val strings = listOf("One","Two","Three","Four","Five")
    val numbers = listOf(1,2,3,4,5)

    val zip1 = strings.zip(numbers)//List<Pair>
    println(zip1)

    println(zip1.unzip())//[(One, 1), (Two, 2), (Three, 3), (Four, 4), (Five, 5)]
    println(zip1.unzip().first)//[One, Two, Three, Four, Five]
    println(zip1.unzip().second)//[1, 2, 3, 4, 5]

4.3 associate()关联

    val strings = listOf("One","Two","Three","Four","Five")
    val numbers = listOf(1,2,3,4,5)

    //map的key值唯一,如果产生重复,后面的将覆盖前面的。
    val ass = strings.associateWith { it.length }
    println(ass)//{One=3, Two=3, Three=5, Four=4, Five=4}

    val ass1 = strings.associate { it to it.length }
    println(ass1)//{One=3, Two=3, Three=5, Four=4, Five=4}
    var ass3 = strings.associate { it.length to it }
    //由于key值有重复值,所以产生了覆盖。
    println(ass3)//{3=Two, 5=Three, 4=Five}

4.4 flattern(),flatMap()

    val containers = listOf(
        listOf("one", "two"),
        listOf("three", "four", "five")
    )

    val flattern1 = containers.flatten()
    println(flattern1)//[one, two, three, four, five]

    val flatMap = containers.flatMap { sublist ->sublist }
    println(flatMap)//[one, two, three, four, five]
    println(containers)//[[one, two], [three, four, five]]

5.遍历 

5.1 range

    val list1 = listOf("One","Two","Three","Four")

    for( i in list1.indices)
        println(list1[i])

    for(i in 0..list1.size-1)
        println(list1[i])

    for( i in 0 until list1.size)
        println(list1[i])

5.2 forEach,forEachIndexed

    val list1 = listOf("One","Two","Three","Four")

    list1.forEach {
        println(it)
    }

    list1.forEachIndexed { index,item -> println("$index: $item") }

5.2 加减操作

    val list1 = mutableListOf("One","Two","Three","Four")

    var list2 = list1+"Five"
    println(list2)

    var list3 = list1 -"One"
    println(list3)

5.3 groupBy(),groupingBy()

val list1 = mutableListOf("One","Two","Three","Four")

var map1 = list1.groupBy { it.first() }
println(map1)//{O=[One], T=[Two, Three], F=[Four]}

var map2 = list1.groupBy { it.length }
println(map2)//{3=[One, Two], 5=[Three], 4=[Four]}

var map3 = list1.groupBy(
    keySelector = {it.length}, valueTransform = {it+"_sdf"}
)
println(map3)//{3=[One_sdf, Two_sdf], 5=[Three_sdf], 4=[Four_sdf]}

var countmap4 = list1.groupingBy { it.length }.eachCount()
println(countmap4)//{3=2, 5=1, 4=1}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lph009

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值