Kotlin日常使用函数记录

文章目录

前言

记录一些kotlin开发中,日常使用的函数和方式之类的,不然有时候总是忘,还得去翻官方文档或者API,必须集合转Map的方式,我总是忘 。。。😭

会不定时进行记录更新,文章也会越来越完善。

字符串

集合

1.两个集合的差集

集合1和集合2,获取集合1中有,但是集合2中没有的

kotlin 复制代码
fun main() {
    val collection1 = listOf(1, 2, 3, 4, 5)
    val collection2 = listOf(3, 4, 5, 6, 7)

    // 使用 minus 操作符计算差集
    val difference = collection1 - collection2
    println("使用 minus 操作符计算的差集: $difference")
}    

2.集合转数组

在 Kotlin 中,可以将集合转换为数组,根据集合元素类型的不同,转换方式也有所不同。这里分别介绍将集合转换为基本数据类型数组和对象数组的方法。

2.1.集合转基本数据类型数组

对于基本数据类型(如 Int、Double 等)的集合,需要使用特定的方法进行转换。Kotlin 提供了相应的转换方法,如 toIntArray()、toDoubleArray()、toLongArray() 等。

集合转 IntArray,使用 toIntArray() 方法将包含 Int 元素的集合转换为 IntArray。

kotlin 复制代码
fun main() {
    // 创建一个整数集合
    val intList = listOf(1, 2, 3, 4, 5)

    // 将集合转换为 IntArray
    val intArray: IntArray = intList.toIntArray()
}    

集合转 DoubleArray,使用 toDoubleArray() 方法将包含 Double 元素的集合转换为 DoubleArray。

kotlin 复制代码
fun main() {
    // 创建一个双精度浮点数集合
    val doubleList = listOf(1.1, 2.2, 3.3, 4.4, 5.5)

    // 将集合转换为 DoubleArray
    val doubleArray: DoubleArray = doubleList.toDoubleArray()
}    

2.2.集合转对象数组

使用 toTypedArray() 方法可以将任意类型的集合转换为对应的对象数组。

kotlin 复制代码
fun main() {
    // 创建一个字符串集合
    val stringList = listOf("apple", "banana", "cherry")

    // 将集合转换为对象数组
    val stringArray: Array<String> = stringList.toTypedArray()
}    

这里使用了String类型的数组,实际中可以是任何对象,例如你自己定义的UserDTO等对象。

Map

1.合并Map

在 Kotlin 里,合并两个 Map 有多种方式。

1.1.使用 + 操作符

此操作符能合并两个Map,若有重复键,后面 Map 的值会覆盖前面的。结果是生成一个新的Map。

kotlin 复制代码
fun main() {
    // 两个Map是不是可变的Map无所谓
    val map1 = mutableMapOf("a" to 1, "b" to 2)
    val map2 = mapOf("b" to 3, "c" to 4)
    val mergedMap = map1 + map2
    println(mergedMap)
}    

使用 plus 函数和 + 操作符作用相同。

kotlin 复制代码
fun main() {
    val map1 = mapOf("a" to 1, "b" to 2)
    val map2 = mapOf("b" to 3, "c" to 4)
    val mergedMap = map1.plus(map2)
    println(mergedMap)
}    

1.2.使用 += 操作符

在有可变 map 的场景下,我们可以使用已有的 map 来保存合并两个 map 后的结果。

kotlin 复制代码
fun main() {
	val map1 = mutableMapOf("a" to 1, "b" to 2)
	val map2 = mapOf("b" to 3, "c" to 4)
	map1 += map2
	println(map1 )
}

1.3.使用 putAll 方法

putAll 方法会把第二个 Map 的键值对添加到第一个可变 Map 中,重复键的值会被覆盖。要求第一个Map必须是可变Map,否则不能调用putAll方法。

kotlin 复制代码
fun main() {
    val mutableMap1 = mutableMapOf("a" to 1, "b" to 2)
    val map2 = mapOf("b" to 3, "c" to 4)
    mutableMap1.putAll(map2)
    println(mutableMap1)
}    

1.4.使用 merge 函数

若有重复键,想自定义处理方式,可使用 merge 函数。

例如,合并两个Map的时候,重复键的值进行相加。

kotlin 复制代码
fun main() {
	// 这里的map1需要是可变的
    val map1 = mutableMapOf("a" to 1, "b" to 2)
    val map2 = mapOf("b" to 3, "c" to 4)
    // 循环map2, 从map1中获取同key的值, 然后加起来, 放入map1中
    map2.forEach { (key, value) ->
    	// oldVal 和 newVal 分别对应于 map1 和 map2 中的值
        map1.merge(key, value) { oldValue, newValue -> oldValue + newValue }
    }
    // {a=1, b=5, c=4}
    println(map1)
}    
相关推荐
朱涛的自习室12 分钟前
我给 Gemini 4 和 DeepSeek 搞了套「末位淘汰」,AI 干活会更猛吗?(Day 3)
android·前端·后端
栈帧之外39 分钟前
【C++】STL之set
开发语言·c++
catchadmin1 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.1 发布,新增 MCP 支持
开发语言·php
墨天梦2 小时前
D19_系统分享与文件URI
gitee·kotlin
jimy13 小时前
c++的 Type Traits (二、更详细版本)
开发语言·c++
狗凯之家源码网3 小时前
纯 PHP+MySQL 应用市场系统源码实测!
开发语言·mysql·php
dishugj3 小时前
系统架构风险点、敏感点和权衡点的概念和区分
java·开发语言
geovindu3 小时前
rust:Memento Pattern(续)
开发语言·后端·设计模式·rust·行为模式·备忘录模
马优晨4 小时前
JAVA 中 VO 实体类是什么意思,为什么要做这个
java·开发语言·状态模式·java vo·java vo是什么
Aurorar0rua4 小时前
CS50 x 2024 Notes Python - 01
开发语言·python·学习方法