当前位置:网站首页>Kotlin combines flatmap for filtering and zip merge operators

Kotlin combines flatmap for filtering and zip merge operators

2022-07-07 08:15:00 yu-Knight

//Kotlin Combination of Chinese and Western Medicine flatMap Perform filtering operations and zip Merge operators 
fun main() {
    val list = listOf<String>(" Huang Xiaoming ", " Jet Li ", " Bruce Lee ", " Liu Jun ", " Li Si ", " Liu Ming ", " Liu Jun ", " Huangjiaju ", " huang feihong ")
    val list2 = list.map {
        "$it"
    }
    println(list2)
    val newList = listOf(
        listOf(" Huang Xiaoming ", " Jet Li ", " Bruce Lee "),
        listOf(" Liu Jun ", " Li Si ", " Liu Ming "),
        listOf(" Liu Jun ", " Huangjiaju ", " huang feihong ")
    )
    newList.flatMap { it ->
        it.filter { it ->
            it.contains(" yellow ") // Inside is true Just go back to ,false  Just filter it out. Don't 
        }
    }.map {
        println("$it  ")
    }
    /*******************************************/
    val names = listOf(" Zhang San ", " Li Si ", " Wang Wu ")
    val ages = listOf(12, 34, 22)
    // principle : Put the first set and the second set , combined , Create a new collection and return 
    // Create a new collection ( Elements , Elements , Elements )  Elements Pair(K,V) K Replace the elements in the first set ,V Represents the elements in the second set 
    val zip: List<Pair<String, Int>> = names.zip(ages)
    println(zip) //[( Zhang San , 12), ( Li Si , 34), ( Wang Wu , 22)]
}

原网站

版权声明
本文为[yu-Knight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130641028308.html