一个copyList的Kotlin代码例子:
2023-09-14 09:07:21 时间
一个copyList的代码例子:
/**
* 把S的列表copy到D的列表
*/
fun <S, D> copyList(sourceList: List<S>?, clazz: Class<D>): List<D>? {
if (null == sourceList || sourceList.size == 0) {
return null
}
val destList = ArrayList<D>()
sourceList.forEach {
val source = it
val dest = createInstance(clazz)
if (dest != null) {
BeanUtils.copyProperties(source, dest)
destList.add(dest)
}
}
return destList
}
private fun <T> createInstance(clazz: Class<T>): T? {
var obj: T?
obj = try {
clazz.newInstance()
} catch (e: Exception) {
logger.error("createInstance with exception: {}", e.message)
logger.error("exception: ", e)
null
}
return obj
}
相关文章
- kotlin中Activity跳转
- 把 "格子衫" 改造得更时尚 | Kotlin & Jetpack 最佳实践技巧
- kotlin获取属性注解
- 开心档-Kotlin 基础语法
- Kotlin安卓开发学习-标准函数和静态方法
- Kotlin 学习笔记(六)—— Flow 数据流学习实践指北(二)StateFlow 与 SharedFlow
- 【Kotlin】Kotlin Sealed 密封类 ( 密封类声明 | 密封类子类定义 | 密封类特点 | 代码示例 )
- 【Kotlin】函数类型 ( 函数类型 | 带参数名称的参数列表 | 可空函数类型 | 复杂函数类型 | 带接收者函数类型 | 函数类型别名 | 函数类型实例化 | 函数调用 )
- 又一巨头从Java迁移到Kotlin:关键应用全部开始切换、安卓代码库超过千万行Kotlin代码
- 【错误记录】Android Studio 编译时 Kotlin 代码编译报错 ( Not enough information to infer type variable T )
- 【开发环境】Android 命令行中执行 Java 程序 ( IntelliJ IDEA 中创建 Java / Kotlin 工程 | dx 打包 DEX 字节码文件 | dalvikvm 命令 )
- 【Kotlin】Kotlin 变量与常量 ( 变量声明 | 只读变量 | 类型推断 | 编译时常量 | Kotlin 字节码查看面板 | Kotlin 引用数据类型 )
- 【Kotlin】标准库函数总结 ( apply 函数 | let 函数 | run 函数 | with 函数 | also 函数 | takeIf 函数 | takeUnless 函数 )
- 【Kotlin】集合操作 ③ ( List 集合遍历 | for in | forEach | forEachIndexed | List 通过解构一次性给多个元素赋值 )
- 【Kotlin】常用的 Kotlin 类 ② ( 枚举类 | 枚举类定义函数 | 密封类 )
- 【Kotlin】接口和抽象类 ( 接口属性和方法定义 | 接口默认方法实现 | 抽象类 )
- 【Kotlin】函数式编程 ③ ( 早集合与惰性集合 | 惰性集合-序列 | generateSequence 序列创建函数 | 序列代码示例 | take 扩展函数分析 )
- Kotlin新增协程、类型别名特性,提升了对JavaScript的支持详解手机开发
- Kotlin 一统天下?Kotlin/Native 支持 iOS 和 Web 开发