1 List
Java 的 List、Set、Map 介绍见 → Java容器及其常用方法汇总。
1.1 创建 List
1.1.1 emptyList
var list = emptyList<String>() // 创建空List
1.1.2 List 构造函数
var list1 = List(3) { "abc" } // [abc, abc, abc]
var list2 = ArrayList<Int>()
var list3 = LinkedList<String>()
1.1.3 listOf
var list1: List<String> = listOf() // 只读List
var list2: MutableList<String> = mutableListOf() // ArrayList
var list3: List<String> = listOf("zhang", "li", "wang")
var list4: MutableList<String> = mutableListOf("zhang", "li", "wang")
var list5 = listOfNotNull(true, 5, 3f, "abc", null)
说明:listof 和 listOfNotNull 生成的列表是只读列表,不能修改。
1.1.4 根据 Array 创建 List
var arr = arrayOf(1, 2, 3)
var list1 = listOf(arr)
var list2 = listOfNotNull(arr)
1.2 增删改查
1.2.1 增加元素
fun main() {
var list = mutableListOf<String>()
list.add("abc")
list.add("xyz")
list.add(1, "ABC")
list += "XYZ"
list += arrayOf("123")
list += listOf("456")
list += setOf("789")
}
1.2.2 删除元素
fun main() {
var list = mutableListOf("abc", "efg", "xyz")
list.remove("abc")
list.removeAt(0)
list -= "xyz"
list -= arrayOf("123")
list -= listOf("456")
list -= setOf("789")
list.clear()
}
1.2.3 修改元素
var list = mutableListOf("abc", "efg", "xyz")
list[0] = "ABC"
1.2.4 访问元素
1)访问元素
fun main() {
var list = mutableListOf("zhan", "li", "wang")
println(list[1]) // 打印: li
list[0] = "chen"
list.any() // true, 判断List是否为空(容量为0)
list.first() // chen, 获取首个元素
list.last() // wang, 获取最后一个元素
list.indexOf("wang") // 2, 查找元素对应的索引
list.binarySearch("wang") // 2, 二分查找元素对应的索引
}
2)遍历元素
fun main() {
var list = mutableListOf("abc", "efg", "xyz")
list.forEach(::println) // abc, efg, xyz
for ((index, item) in list.withIndex()) {
println("[$index, $item]") // [0, abc], [1, efg], [2, xyz]
}
}
1.3 修改元素顺序
1.3.1 翻转元素
fun main() {
var list1 = mutableListOf(1, 2, 3, 4, 5)
var list2 = list1.reversed() // 翻转List元素顺序, 并生成新的List
list1.reverse() // 在原List上翻转数组元素顺序
}
1.3.2 打乱元素顺序
fun main() {
var list1 = mutableListOf(1, 2, 3, 4, 5)
var list2 = list1.shuffled() // 打乱List元素顺序, 并生成新的List
list1.shuffle() // 打乱List元素顺序
}
1.3.3 元素排序
fun main() {
var list1 = mutableListOf(3, 1, 5, 2, 4)
var list2 = list1.sorted() // 从小到大排序, 并生成新的List
list1.sort() // 从小到大排序
var list3 = list1.sortedDescending() // 从大到小排序, 并生成新的List
list1.sortDescending() // 从大到小排序
}
1.4 拼接列表元素
fun main() {
var list = listOf("aa", "bb", "cc")
var str1 = list.joinToString()
println(str1) // 打印: aa, bb, cc
var str2 = list.joinToString("-", "[", "]")
println(str2) // 打印: [aa-bb-cc]
var str3 = list.joinToString(limit = 2, truncated = "***")
println(str3) // 打印: aa, bb, ***
var str4 = list.joinToString("/") {
(it + it)
}
println(str4) // 打印: aaaa/bbbb/cccc
}
1.5 切片
fun main() {
var list1 = listOf("AA", "BBB", "CC", "DDD")
var list2 = list1.slice(1..2) // [BBB, CC], 截取1,2元素
var list3 = list1.drop(2) // [CC, DDD], 丢弃前2个元素
var list4 = list1.dropLast(2) // [AA, BBB], 丢弃最后2个元素
var list5 = list1.take(2) // [AA, BBB], 截取前2个元素
var list6 = list1.takeLast(2) // [CC, DDD], 截取最后2个元素
}
1.6 统计函数
fun main() {
var list = mutableListOf(1, 2, 3)
list.sum() // 6
list.average() // 2.0
list.max() // 3
list.min() // 1
list.count() // 3
}
1.7 集合运算
fun main() {
var list1 = listOf("AAA", "BBB")
var list2 = listOf("BBB", "CCC")
println(list1 + list2) // [AAA, BBB, BBB, CCC]
println(list1 - list2) // [AAA]
println(list1 union list2) // [AAA, BBB, CCC]
println(list1 intersect list2) // [BBB]
println(list1 subtract list2) // [AAA]
println(list2 subtract list1) // [CCC]
println((list1 - list2) union (list2 - list1)) // [AAA, CCC]
}
1.8 map
1.8.1 map
fun main() {
var list1 = listOf(5, 6, 7)
var list2 = list1.map { it * it } // [25, 36, 49]
}
1.8.2 mapIndexed
fun main() {
var list1 = listOf(5, 6, 7)
var list2 = list1.mapIndexed { index, value ->
"[$index, $value]" // [[0, 5], [1, 6], [2, 7]]
}
}
1.9 groupBy
fun main() {
var list = listOf("AA", "BBB", "CC", "DDD")
// {2=[AA, CC], 3=[BBB, DDD]}
var map: Map<Int, List<String>> = list.groupBy { it.length }
}
1.10 zip
1.10.1 zip
fun main() {
var list1 = listOf(1, 2, 3)
var list2 = listOf("AAA", "BBB", "CCC")
// [(1, AAA), (2, BBB), (3, CCC)]
var pairs: List<Pair<Int, String>> = list1.zip(list2)
}
1.10.2 unzip
fun main() {
// [(1, AAA), (2, BBB), (3, CCC)]
var pairList: List<Pair<Int, String>> = listOf(
1 to "AAA",
2 to "BBB",
3 to "CCC"
)
// ([1, 2, 3], [AAA, BBB, CCC])
var listPair: Pair<List<Int>, List<String>> = pairList.unzip()
}
1.11 associate
fun main() {
var list = listOf("AAA", "BBBBB", "CCCC")
// {AAA=3, BBBBB=5, CCCC=4}
var map1: Map<String, Int> = list.associateWith { it.length }
// {3=AAA, 5=BBBBB, 4=CCCC}
var map2: Map<Int, String> = list.associateBy { it.length }
// {3=AAA, 5=BBBBB, 4=CCCC}
var map3: Map<String, Int> = list.associate { it to it.length }
}
1.12 flatten
1.12.1 flatten
fun main() {
var list = listOf(listOf("AA", "BB"), listOf("CC", "DD"))
var flatten: List<String> = list.flatten() // [AA, BB, CC, DD]
}
1.12.2 flatMap
fun main() {
var list = listOf(Container(listOf("AA")), Container(listOf("BB")))
var flatten: List<String> = list.flatMap { it.list } // [AA, BB]
}
class Container(var list: List<String>)
1.13 chunked
fun main() {
var list1 = listOf("AA", "BBB", "CC", "DDD", "E", "FF", "GGG")
var list2 = list1.chunked(3) // [[AA, BBB, CC], [DDD, E, FF], [GGG]]
}
2 Set
2.1 创建 Set
2.1.1 emptySet
var set = emptySet<String>()
2.1.2 Set 构造函数
var set1 = HashSet<Int>()
var set2 = LinkedHashSet<String>()
var set3 = TreeSet<String>()
2.1.3 setOf
var set1: Set<String> = setOf() // 只读Set
var set2: MutableSet<String> = mutableSetOf() // LinkedHashSet
var set3: HashSet<String> = hashSetOf() // HashSet
var set4: LinkedHashSet<String> = linkedSetOf() // LinkedHashSet
var set5: TreeSet<String> = sortedSetOf() // TreeSet
var set6: Set<String> = setOf("zhang", "li", "wang")
var set7: MutableSet<String> = mutableSetOf("zhang", "li", "wang")
var set8 = setOfNotNull(true, 5, 3f, "abc", null)
2.1.4 根据 Array 创建 Set
var arr = arrayOf(1, 2, 3)
var set1 = setOf(arr)
var set2 = setOfNotNull(arr)
2.2 增删改查
2.2.1 增加元素
fun main() {
var set = mutableSetOf<String>()
set.add("abc")
set.add("xyz")
set += "ABC"
set += arrayOf("123")
set += listOf("456")
set += setOf("789")
}
2.2.2 删除元素
fun main() {
var set = mutableListOf("abc", "efg", "xyz")
set.remove("abc")
set.removeAt(0)
set -= "xyz"
set -= arrayOf("123")
set -= listOf("456")
set -= setOf("789")
set.clear()
}
2.2.3 访问元素
1)访问元素
fun main() {
var set = mutableSetOf("zhan", "li", "wang")
println(set.elementAt(1)) // 打印: li
set.any() // true, 判断List是否为空(容量为0)
set.first() // chen, 获取首个元素
set.last() // wang, 获取最后一个元素
set.indexOf("wang") // 2, 查找元素对应的索引
}
2)遍历元素
fun main() {
var set = mutableSetOf("abc", "efg", "xyz")
set.forEach(::println) // abc, efg, xyz
for ((index, item) in set.withIndex()) {
println("[$index, $item]") // [0, abc], [1, efg], [2, xyz]
}
}
2.3 修改元素顺序
2.3.1 翻转元素
fun main() {
var set = mutableSetOf(1, 2, 3, 4, 5)
var list = set.reversed() // 翻转Set元素顺序, 并生成新的List
}
2.3.2 打乱元素顺序
fun main() {
var set = mutableSetOf(1, 2, 3, 4, 5)
var list = set.shuffled() // 打乱Set元素顺序, 并生成新的List
}
2.3.3 元素排序
fun main() {
var set = mutableSetOf(3, 1, 5, 2, 4)
var list1 = set.sorted() // 从小到大排序, 并生成新的List
var list2 = set.sortedDescending() // 从大到小排序, 并生成新的List
}
2.4 统计函数
fun main() {
var set = mutableSetOf(1, 2, 3)
set.sum() // 6
set.average() // 2.0
set.max() // 3
set.min() // 1
set.count() // 3
}
2.5 集合运算
fun main() {
var set1 = setOf("AAA", "BBB")
var set2 = setOf("BBB", "CCC")
println(set1 + set2) // [AAA, BBB, BBB, CCC]
println(set1 - set2) // [AAA]
println(set1 union set2) // [AAA, BBB, CCC]
println(set1 intersect set2) // [BBB]
println(set1 subtract set2) // [AAA]
println(set2 subtract set1) // [CCC]
println((set1 - set2) union (set2 - set1)) // [AAA, CCC]
}
3 Map
3.1 创建 Map
3.1.1 Pair
1)Pair 构造函数
public data class Pair<out A, out B>(
public val first: A,
public val second: B
) : Serializable
2)to 函数
public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)
3)应用
var pair: Pair<Int, String> = 1001 to "Tom"
3.1.2 emptyMap
var map = emptyMap<Int, String>()
3.1.3 Map 构造函数
var map1 = HashMap<Int, String>()
var map2 = Hashtable<Int, String>()
var map3 = LinkedHashMap<Int, String>()
var map4 = ConcurrentHashMap<Int, String>()
var map5 = TreeMap<Int, String>()
3.1.4 mapOf
var map: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
3.2 增删改查
3.2.1 增加元素
1)put
fun main() {
var map: MutableMap<Int, String> = mutableMapOf()
map.put(1001, "Tom")
}
2)map[key] = value
fun main() {
var map: MutableMap<Int, String> = mutableMapOf()
map[1002] = "Mary"
}
2)+
fun main() {
var map: MutableMap<Int, String> = mutableMapOf()
map += 1001 to "John"
map += arrayOf(1002 to "Zhang")
map += listOf(1003 to "Wang")
map += setOf(1004 to "Li")
map += mapOf(1005 to "Xie")
}
4)putAll
fun main() {
var map: MutableMap<Int, String> = mutableMapOf()
var list1 = listOf(1, 2, 3)
var list2 = listOf("zhang", "li", "wang")
map.putAll(list1.zip(list2))
}
3.2.2 删除元素
fun main() {
var map: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
map.remove(1001)
map -= 1002
map -= arrayOf(1003)
map -= listOf(1004)
map -= setOf(1005)
map.clear()
}
3.2.3 修改元素
var map: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
map[1001] = "John"
3.2.4 访问元素
1)访问元素
var map: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
println(map[1001]) // Tom
2)遍历元素
fun main() {
var map: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
map.forEach { key, value ->
println("[$key, $value]") // [1001, Tom], [1002, Mary]
}
for ((key, value) in map) {
println("[$key, $value]") // [1001, Tom], [1002, Mary]
}
}
3.3 keys 和 values
3.3.1 keys、values、entries
fun main() {
var map: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
println(map.keys) // [1001, 1002]
println(map.values) // [Tom, Mary]
for (entry in map.entries) {
println("[${entry.key}, ${entry.value}]") // [1001, Tom], [1002, Mary]
}
println(map.containsKey(1001)) // true
println(map.containsValue("Tom")) // true
}
3.3.2 mapKeys、mapValues
fun main() {
var map1: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
var map2 = map1.mapKeys { it.key - 1000 } // {1=Tom, 2=Mary}
var map3 = map1.mapValues { it.value + "X" } // {1001=TomX, 1002=MaryX}
}
3.4 getOrDefault、getOrElse、getOrPut
fun main() {
var map: MutableMap<Int, String> = mutableMapOf(
1001 to "Tom",
1002 to "Mary"
)
// 在没有结果时返回给定的默认值
var name1 = map.getOrDefault(1001, "xxx")
// 使用函数式返回默认值
var name2 = map.getOrElse(1001) { "xxx" }
// 添加(1003, John), 并返回John
var name3 = map.getOrPut(1003) { "John" }
}
声明:本文转自【Kotlin】List、Set、Map简介。
标签:map,Set,list1,Kotlin,list,fun,var,main,List From: https://www.cnblogs.com/zhyan8/p/18115110