kotlin练习 ---- 集合练习

kotlin练习 - 集合练习

Set集合

  • Set集合创建
fun main(args: Array) {
    //创建不可变集合,返回Set
    var set = setOf("java", "kotlin", "go")
    println(set)//[java, kotlin, go]
    println("setOf 返回类型 ${set.javaClass}")

    //创建可变集合,返回值MutableSet
    var mutablemap = mutableListOf("java", "kotlin", "go")
    mutablemap[0] = "JavaScript"
    println(mutablemap)//[JavaScript, kotlin, go]
    println("mutableListOf 返回类型 ${mutablemap.javaClass}")

    //创建LinkedHashSet集合
    var linkedHashSet = linkedSetOf("java", "kotlin", "go")
    println(linkedHashSet)
    println("linkedHashSet 返回类型 ${linkedHashSet.javaClass}")

    //创建HashSet集合
    var hashSet = hashSetOf("java", "kotlin", "go")
    println(hashSet)//不保证元素的顺序--[kotlin, go, java]

    //创建TreeSet集合
    var treeSet = sortedSetOf("java", "kotlin", "go")
    println(treeSet)//集合按从小到大排列--[go, java, kotlin]
}
  • Set集合的使用
   //创建不可变集合,返回Set
    var set = setOf("java", "kotlin", "go")

    //判断是否所有的元素长度都大于5  ------所有元素都满足条件
    println(set.all { it.length > 5 })// false

    //判断是否任意一元素的长度大于5  ------任意元素都满足条件
    println(set.any { it.length > 5 })//true

    //以Lambda 表达式的值为key , 集合元素为value ,组成的Map集合
    var map = set.associateBy({ "我正在学习${it}" })
    println(map)//{我正在学习java=java, 我正在学习kotlin=kotlin, 我正在学习go=go}

    //使用in、!in运算符
    println("java" in set)// true
    println("java" !in set)// false

    //删除Set集合前面两个元素
    var dropedList = set.drop(2)
    println(dropedList)// [go]

    //对Set集合进行过滤: 需要得到所有包含 va 的集合,
    var filteredList1 = set.filter({ "va" in it })//如果没有返回空元素的集合
    println(filteredList1)//[java]

    //查找Set集合中包含 va 的元素,如果找到就返回该元素,否则返回 null
    var filteredList2 = set.find({ "o" in it })
    println(filteredList2)//kotlin
    var filteredList3 = set.find({ "va1" in it })
    println(filteredList3)//null

    //将Set集合中所有字符串拼接在一起
    var foldedList1 = set.fold("", { acc, s -> acc + s })
    println(foldedList1)//javakotlingo
    var foldedList2 = set.fold("456", { acc, s -> acc +" 123 ${s}"  })
    println(foldedList2)//456 123 java 123 kotlin 123 go
    //查找某个元素的出现位置------没有就会返回-1
    println(set.indexOf("kotlin"))// 1
    println(set.indexOf("kotlin1"))// -1
    
      //循环遍历
    var books = setOf("疯狂java讲义", "疯狂kotlin讲义", "疯狂IOS讲义", "疯狂android讲义")
    for (book in books){
        println(book)
    }
    books.forEach({
        println(it)
    })
    for (i in books.indices){
        println(books.elementAt(i))
    }
    
    var books = mutableSetOf("疯狂java讲义")
      //新增
    books.addAll(setOf("疯狂kotlin讲义", "疯狂IOS讲义"))
    books.add("疯狂android讲义")
    println(books.toString())//[疯狂java讲义, 疯狂kotlin讲义, 疯狂IOS讲义, 疯狂android讲义]
    //删除
    books.remove("疯狂kotlin讲义")
    println(books.toString())//[疯狂java讲义,疯狂IOS讲义, 疯狂android讲义]
    books.removeAll(setOf("疯狂java讲义"))
    println(books.toString())//[疯狂IOS讲义, 疯狂android讲义]
    //清空所有
    books.clear()
    println(books.toString())//[]
    //只保留公共的元素
    books = mutableSetOf("疯狂java讲义", "疯狂kotlin讲义", "疯狂IOS讲义", "疯狂android讲义")
    books.retainAll(setOf("疯狂kotlin讲义", "疯狂PHP讲义", "疯狂android讲义"))
    println(books.toString())//[疯狂kotlin讲义, 疯狂android讲义]

List集合

  • List集合创建
    var list1 = listOf(1, 2, 3, 4, 5, null, 7, 8, 9)
    println(list1.toString())//[1, 2, 3, 4, 5, null, 7, 8, 9]
    var list2 = listOfNotNull(1, 2, 3, 4, 5, null, 7, 8, 9)
    println(list2.toString())//[1, 2, 3, 4, 5, 7, 8, 9]
    var list3 = mutableListOf(1, 2, 3, 4, 5, null, 7, 8, 9)
    println(list3.toString())//[1, 2, 3, 4, 5, null, 7, 8, 9]
    list3.add(10)
    list3.set(5, 6)
    println(list3.toString())//[1, 2, 3, 4, 5, 6, 7, 8, 9,10]
    var list4 = arrayListOf(1, 2, 3, 4, 5, null, 7, 8, 9)
    println(list4.toString())//[1, 2, 3, 4, 5, null, 7, 8, 9]
    list4.set(5, 6)
    list4.add(10)
    println(list4.toString())//[1, 2, 3, 4, 5, 6, 7, 8, 9,10]
  • List集合的使用
 //遍历List
    var list1 = listOf("Java", null, "Kotlin", "Go")
    list1.forEach {
        println(it)
    }
    for (str in list1) {
        println(str)
    }
    for (i in list1.indices) {
        println(list1[i])
        println("get = ${list1.get(i)}")
        println("elementAt = ${list1.elementAt(i)}")
    }
    var index = list1.indexOf("Go")
    println("Go 在数组中的位置:${index}")//3

    var list2 = mutableListOf(1, 2, 3, 1, 8, 3, 4, 6, 1, 4, 2, 3)
    //最后一次出现的的位置
    var last1 = list2.lastIndexOf(2)//10
    //最后一次出现的的位置
    var last2 = list2.indexOfLast { it == 2 }//10
    //第一次出现的位置
    var first = list2.indexOfFirst { it == 2 }//1
    println("2在数组中第一次出现在${first},最后一次出现在${last1},${last2}")
    //返回集合
    var list3 = listOf("Java", null, "Kotlin", "PHP", null, "Go")
    var sublist = list3.subList(2, 5)//从第2个到第5个之间的元素
    println(sublist.toString())//[Kotlin, PHP, null]
//    var list4 = mutableListOf("Java", "JavaScript", null, "HTML", "Kotlin", null, "Python", "PHP", "Go")
    var list4 = mutableListOf("Java", null, "HTML", "Go")
    println(list4)//[Java, null, HTML, Go]
    //新增
    list4.add("Python")
    list4.add(2, "C++")
    list4.addAll(listOf("Kotlin", null))
    println(list4)//[Java, null, C++, HTML, Go, Python, Kotlin, null]
    //删除
    list4.removeAt(1)
    println(list4)//[Java, C++, HTML, Go, Python, Kotlin, null]
    //list4[6] ="CSS";
    list4.set(6, "CSS")
    println(list4)//[Java, C++, HTML, Go, Python, Kotlin, CSS]
    list4.remove("Go")
    println(list4)//[Java, C++, HTML, Python, Kotlin, CSS]
    list4.add(3, "Go")
    list4.add(5, "Go")
    println(list4)//[Java, C++, HTML, Go, Python, Go, Kotlin, CSS]
    //替换元素
    list4.replaceAll {
        if (it.equals("Go")) {
            "go"
        } else {
            it
        }
    }
    println(list4)//[Java, C++, HTML, go, Python, go, Kotlin, CSS] 
    //删除
    list4.removeAll(listOf("go"))
    println(list4)//[Java, C++, HTML, Python, Kotlin, CSS]

    //清空
    list4.clear()
    println(list4)//[]

Map集合

  • Map集合创建
    //不可变map集合
    var map1 = mapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4)
    println(map1)//{Java=1, JavaScript=2, HTML=3, Kotlin=4}
    //可变集合MutableMap
    var map2 = mutableMapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4)
    println(map2)//{Java=1, JavaScript=2, HTML=3, Kotlin=4}
    map2.put("Python", 5)
    println(map2)//{Java=1, JavaScript=2, HTML=3, Kotlin=4, Python=5}
    map2.remove("JavaScript")
    println(map2)//{Java=1, HTML=3, Kotlin=4, Python=5}
    //HashMap集合----不保证key-value的顺序
    var map3 = hashMapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4)
    println(map3)//{Java=1, HTML=3, JavaScript=2, Kotlin=4}
    //LinkedHashMap集合---key-value按添加顺序排列
    var map4 = linkedMapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4)
    println(map4)//{Java=1, JavaScript=2, HTML=3, Kotlin=4}
    //SortedMap集合------ key-value按key由小到大排列
    var map5 = sortedMapOf("Java" to 1, "Python" to 5, "HTML" to 3, "Kotlin" to 4, "JavaScript" to 2, "Go" to 6)
    println(map5)//{Go=6, HTML=3, Java=1, JavaScript=2, Kotlin=4, Python=5}

  • Map集合的使用
    var map1 = mapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4)
    //当map集合中所有key-value对都满足条件返回true
    var all = map1.all {
        it.key.length >= 4 && it.value > 0
    }
    println(all)//true
    //当map集合中任意一个key-value对满足条件就会返回true
    var any = map1.any {
        it.key.length >= 10 && it.value > 1
    }
    println(any)//true
    //判断map集合中是否有对于key的key-value
    println("Java" in map1);//true
    println("Java" !in map1);//false
    println("Go" in map1);//false
    println("Go" !in map1);//true

    //对map进行过滤,要求key中包含:Java
    var filterMap1 = map1.filter { "Java" in it.key }
    println(filterMap1)//{Java=1, JavaScript=2}
    var filterMap2 = map1.filter { it.key in "Java" }
    println(filterMap2)//{Java=1}
    //通过map集合,返回一个新的List
    var mappedList1 = map1.map { "《疯狂${it.key}》讲义,第${it.value}个" }
    var mappedList2 = map1.map { "《疯狂${it.key}》讲义" to "第${it.value}个" }
    println(mappedList1)//[《疯狂Java》讲义,第1个, 《疯狂JavaScript》讲义,第2个, 《疯狂HTML》讲义,第3个, 《疯狂Kotlin》讲义,第4个]
    println(mappedList2)//[(《疯狂Java》讲义, 第1个), (《疯狂JavaScript》讲义, 第2个), (《疯狂HTML》讲义, 第3个), (《疯狂Kotlin》讲义, 第4个)]
    //Map集合中key-value对中 value最大的值
    var maxby1 = map1.maxBy { it.value }
    println(maxby1)//Kotlin=4

    //Map集合中key-value对中 value最小的值
    var minby1 = map1.minBy { it.value }
    println(minby1)//Java=1

    //Map集合中key-value对中 key的长度最大的值
    var maxby2 = map1.maxBy { it.key.length }
    println(maxby2)//JavaScript=2

    //Map集合中key-value对中 key的长度最小的值
    var minby2 = map1.minBy { it.key.length }
    println(minby2)//Java=1

    var map2 = mapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4)
    var map3 = mapOf("Python" to 1, "Go" to 2, "HTML" to 3, "Kotlin" to 4)
    //集合相加---相当于并集
    println(map2 + map3)//{Java=1, JavaScript=2, HTML=3, Kotlin=4, Python=1, Go=2}
    //集合相减---减去公共的元素
    println(map2 - map3)//{Java=1, JavaScript=2, HTML=3, Kotlin=4}

    var plus = map2.plus(map3)
    println(plus)//{Java=1, JavaScript=2, HTML=3, Kotlin=4, Python=1, Go=2}

    var minus = map2.minus(map3)
    println(minus)//{Java=1, JavaScript=2, HTML=3, Kotlin=4}
    //遍历Map集合
    var map4 = mapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4, "Go" to 5)
    for (en in map4.entries) {
        println("${en.key} -> ${en.value}")
    }
    for (key in map4.keys) {
        println("${key} -> ${map4[key]}")
    }
    for ((key, value) in map4) {
        println("${key} -> ${value}")
    }
    map4.forEach({
        println("${it.key} -> ${it.value}")
    })

    var map5 = mutableMapOf("Java" to 1, "JavaScript" to 2, "HTML" to 3, "Kotlin" to 4, "Go" to 5)
    map5["Go"] = 0
    println(map5)//{Java=1, JavaScript=2, HTML=3, Kotlin=4, Go=0}
    map5.put("HTML", 10)
    println(map5)//{Java=1, JavaScript=2, HTML=10, Kotlin=4, Go=0}
    map5.remove("Go")
    println(map5)//{Java=1, JavaScript=2, HTML=10, Kotlin=4}
    map5.remove("Java", 2)
    println(map5)//{Java=1, JavaScript=2, HTML=10, Kotlin=4}
    map5.remove("Java", 1)
    println(map5)//{JavaScript=2, HTML=10, Kotlin=4}
    map5.putAll(hashMapOf("PHP" to 6, "C++" to 7,"JavaScript" to 8 ))
    println(map5)//{JavaScript=8, HTML=10, Kotlin=4, PHP=6, C++=7}
    //清空Map集合
    map5.clear()
    println(map5)//{}

你可能感兴趣的:(kotlin练习 ---- 集合练习)