一、创建聚集
创建聚集的最常用方法是利用尺度库函数 listOf<T>()、setOf<T>()、mutableListOf<T>()、mutableSetOf<T>()。 假如以逗号分隔的聚集元素列表作为参数,编译器会主动检测元素范例。创建空集适时,须明确指定范例。
val numbersSet = setOf("one", "two", "three", "four")val emptySet = mutableSetOf<String>()Map 也有如许的函数 mapOf()与 mutableMapOf()。映射的键和值作为 Pair 对象转达(通常利用中缀函数 to 创建)。
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)留意,to 符号创建了一个短时存活的 Pair 对象,因此发起仅在性能不告急时才利用它。为制止过多的内存利用,请利用其他方法。比方,可以创建可写 Map 并利用写入利用添补它。 apply()函数可以帮助保持初始化流畅。
val numbersMap = mutableMapOf<String, String>().apply { this["one"] = "1"; this["two"] = "2" }还有效于创建没有任何元素的聚集的函数:emptyList()、emptySet()与 emptyMap()。 创建空集适时,应指定聚集将包罗的元素范例。
val empty = emptyList<String>()详细范例构造函数:
val linkedList = LinkedList<String>(listOf("one", "two", "three"))val presizedSet = HashSet<Int>(32)复制:
在特定时候通过聚集复制函数,比方toList()、toMutableList()、toSet()等等。创建了聚集的快照。 结果是创建了一个具有雷同元素的新聚集 假如在源聚集中添加或删除元素,则不会影响副本。副本也可以独立于源聚集举行更改。
这些函数还可用于将聚集转换为其他范例,比方根据 List 构建 Set:
val sourceList = mutableListOf(1, 2, 3) val copySet = sourceList.toMutableSet()copySet.add(3)copySet.add(4) println(copySet)过滤:
val numbers = listOf("one", "two", "three", "four") val longerThan3 = numbers.filter { it.length > 3 }println(longerThan3)二、迭代器
通过迭代器遍历聚集:
val numbers = listOf("one", "two", "three", "four")val numbersIterator = numbers.iterator()while (numbersIterator.hasNext()) { println(numbersIterator.next())}遍历 Iterable 聚集的另一种方法是众所周知的 for 循环:
val numbers = listOf("one", "two", "three", "four")for (item in numbers) { println(item)}通过forEach遍历:
val numbers = listOf("one", "two", "three", "four")numbers.forEach { println(it)}List 迭代器:
对于列表,有一个特殊的迭代器实现: ListIterator它支持列表双向迭代:正向与反向。 反向迭代由 hasPrevious()和 previous()函数实现。 别的, ListIterator 通过 nextIndex() 与 previousIndex()函数提供有关元素索引的信息。
val numbers = listOf("one", "two", "three", "four")val listIterator = numbers.listIterator()while (listIterator.hasNext()) listIterator.next()println("Iterating backwards:")while (listIterator.hasPrevious()) { print("Index: ${listIterator.previousIndex()}") println(", value: ${listIterator.previous()}")}具有双向迭代的本事意味着 ListIterator 在到达末了一个元素后仍可以利用。
可变迭代器:
为了迭代可变聚集,于是有了 MutableIterator来扩展 Iterator 使其具有元素删除函数 remove()。因此,可以在迭代时从聚集中删除元素。
val numbers = mutableListOf("one", "two", "three", "four") val mutableIterator = numbers.iterator()mutableIterator.next()mutableIterator.remove() println("After removal: $numbers")除了删除元素, MutableListIterator还可以在迭代列表时插入和更换元素。
val numbers = mutableListOf("one", "four", "four") val mutableListIterator = numbers.listIterator()mutableListIterator.next()mutableListIterator.add("two")mutableListIterator.next()mutableListIterator.set("three") println(numbers)三、序列
除了聚集之外,Kotlin 尺度库还包罗另一种容器范例——序列(Sequence<T>)。 序列提供与 Iterable 雷同的函数。
创建序列:
val numbersSequence = sequenceOf("four", "three", "two", "one")将 Iterable 转成 Sequence:
val numbers = listOf("one", "two", "three", "four")val numbersSequence = numbers.asSequence()基于函数创建无穷序列:
val oddNumbers = generateSequence(1) { it + 2 } // 创建无穷序列println(oddNumbers.take(6).toList()) // 取前6个元素,返回长度为6的序列基于函数创建有限序列:
val oddNumbersLessThan10 = generateSequence(1) { if (it + 2 < 10) it + 2 else null }println(oddNumbersLessThan10.count())Iterable 与 Sequence 之间的区别?
Iterable:
val words = "The quick brown fox jumps over the lazy dog".split(" ")val lengthsList = words.filter { println("filter: $it"); it.length > 3 } .map { println("length: ${it.length}"); it.length } .take(4)
Sequence:
val words = "The quick brown fox jumps over the lazy dog".split(" ")// 将列表转换为序列val wordsSequence = words.asSequence()val lengthsSequence = wordsSequence.filter { println("filter: $it"); it.length > 3 } .map { println("length: ${it.length}"); it.length } .take(4)
四、聚集转换
【1】利用映射(map)转换:
val numbers = setOf(1, 2, 3)println(numbers.map { it * 3 })println(numbers.mapIndexed { idx, value -> value * idx })输出结果:
[3, 6, 9][0, 2, 6]转换过程中大概为null,为了防止null值,可用 mapNotNull() 函数代替 map() 或 mapIndexedNotNull() 代替 mapIndexed(),来从结果会合过滤掉 null 值。
val numbers = setOf(1, 2, 3)println(numbers.mapNotNull { if ( it == 2) null else it * 3 })println(numbers.mapIndexedNotNull { idx, value -> if (idx == 0) null else value * idx })输出结果:
[3, 9][2, 6]映射转换时,有两个选择:转换键,使值保持稳固,反之亦然。
要将指定转换应用于键,请利用 mapKeys();反过来,mapValues() 转换值。 这两个函数都利用将映射条目作为参数的转换,因此可以利用其键与值。
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)println(numbersMap.mapKeys { it.key.toUpperCase() })println(numbersMap.mapValues { it.value + it.key.length })输出结果:
{KEY1=1, KEY2=2, KEY3=3, KEY11=11}{key1=5, key2=6, key3=7, key11=16}【2】利用合拢(zip)转换
合拢转换是根据两个聚集中具有雷同位置的元素构建配对。
val colors = listOf("red", "brown", "grey")val animals = listOf("fox", "bear", "wolf")println(colors zip animals) // 方式一val twoAnimals = listOf("fox", "bear")println(colors.zip(twoAnimals)) // 方式二输出结果:
[(red, fox), (brown, bear), (grey, wolf)][(red, fox), (brown, bear)]利用带有两个参数的转换函数来调用 zip():
val colors = listOf("red", "brown", "grey")val animals = listOf("fox", "bear", "wolf")println(colors.zip(animals) { color, animal -> "The ${animal.capitalize()} is $color"})输出结果:
[The Fox is red, The Bear is brown, The Wolf is grey]利用 unzipping 反向转换:
val numberPairs = listOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)println(numberPairs.unzip())输出结果:
([one, two, three, four], [1, 2, 3, 4])【3】利用关联(associate)转换
associateWith(原始聚集是键) :
val numbers = listOf("one", "two", "three", "four") // 键println(numbers.associateWith { it.length })输出结果:
{one=3, two=3, three=5, four=4}associateBy (原始聚集是值):
val numbers = listOf("one", "two", "three", "four") // 值println(numbers.associateBy { it.first().toUpperCase() })println(numbers.associateBy(keySelector = { it.first().toUpperCase() }, valueTransform = { it.length }))输出结果:
{O=one, T=three, F=four}{O=3, T=5, F=4}associate (目标聚集的键和值都是通过原始聚集天生的):
data class FullName (val firstName: String, val lastName: String)fun parseFullName(fullName: String): FullName { val nameParts = fullName.split(" ") if (nameParts.size == 2) { return FullName(nameParts[0], nameParts[1]) } else throw Exception("Wrong name format")}val names = listOf("Alice Adams", "Brian Brown", "Clara Campbell")println(names.associate { name -> parseFullName(name).let { it.lastName to it.firstName } })输出结果:
{Adams=Alice, Brown=Brian, Campbell=Clara}【4】利用打平(flat)转换(将一个聚集转换成另一个聚集)
利用 flatten 将嵌套聚集转成非嵌套:
val numberSets = listOf(setOf(1, 2, 3), setOf(4, 5, 6), setOf(1, 2))println(numberSets.flatten())输出结果:
[1, 2, 3, 4, 5, 6, 1, 2]合并两字节数组:
val array1 = UByteArray(1)array1[0] = 0x01uval array2 = UByteArray(1)array2[0] = 0x02uval array3 = listOf(array1, array2).flatten().toUByteArray().asByteArray()for (i in array3.indices) { print(array3.toString() + " ")}利用flatMap处置惩罚嵌套聚集:
data class StringContainer(val values: List<String>)val containers = listOf( StringContainer(listOf("one", "two", "three")), StringContainer(listOf("four", "five", "six")), StringContainer(listOf("seven", "eight")))println(containers.flatMap { it.values })输出结果:
[one, two, three, four, five, six, seven, eight]五、过滤
根据值过滤:
val numbers = listOf("one", "two", "three", "four") val longerThan3 = numbers.filter { it.length > 3 }println(longerThan3)val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10}println(filteredMap)输出结果:
[three, four]{key11=11}根据索引和值来过滤:
val numbers = listOf("one", "two", "three", "four")val filteredIdx = numbers.filterIndexed { index, s -> (index != 0) && (s.length < 5) }println(filteredIdx)输出结果:
[two, four]根据否定条件来过滤聚集:
val numbers = listOf("one", "two", "three", "four")val filteredNot = numbers.filterNot { it.length <= 3 }println(filteredNot)输出结果:
[three, four]根据指定范例过滤:
val numbers = listOf(null, 1, "two", 3.0, "four")println("All String elements in upper case:")numbers.filterIsInstance<String>().forEach { println(it.toUpperCase())}输出结果:
All String elements in upper case:TWOFOUR过滤全部非空值:
val numbers = listOf(null, "one", "two", null)numbers.filterNotNull().forEach { println(it) }输出结果:
onetwo通过一个谓词过滤聚集并且将不匹配的元素存放在一个单独的列表中:
val numbers = listOf("one", "two", "three", "four")val (match, rest) = numbers.partition { it.length > 3 }println(match)println(rest)输出结果:
[three, four][one, two]检验谓词:
val numbers = listOf("one", "two", "three", "four")println(numbers.any { it.endsWith("e") }) // 至少有一个元素匹配给定谓词println(numbers.none { it.endsWith("a") }) // 没有元素与给定谓词匹配println(numbers.all { it.endsWith("e") }) // 全部元素都匹配给定谓词六、加减利用符
在 Kotlin 中,为聚集界说了 plus (+) 和 minus(-) 利用符。它们均为重载利用符, plus对应着+,minus对应着-。
val numbers = listOf("one", "two", "three", "four")val plusList = numbers + "five"val minusList = numbers - listOf("three", "four")println(plusList)println(minusList)输出结果:
[one, two, three, four, five][one, two]七、分组
根本函数 groupBy() 利用一个 lambda 函数并返回一个 Map,在此 Map 中,每个键都是 lambda 结果,而对应的值是返回此结果的元素 List。
按大写首字母分组:
val numbers = listOf("one", "two", "three", "four", "five")println(numbers.groupBy { it.first().toUpperCase() })输出结果:
{O=[one], T=[two, three], F=[four, five]}按首字母分组,并将值转成大写:
val numbers = listOf("one", "two", "three", "four", "five")println(numbers.groupBy(keySelector = { it.first() }, valueTransform = { it.toUpperCase() }))输出结果:
{o=[ONE], t=[TWO, THREE], f=[FOUR, FIVE]}假如要多详细的元素举行分组,可以利用groupingBy:
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.groupingBy { it.first() }.eachCount())输出结果:
{o=1, t=2, f=2, s=1}groupingBy 返回Grouping范例,Grouping有 fold 和 reduce 函数。
fold 和 reduce:
它们的区别是?
reduce的返回值范例必须和聚集的元素范例符合。fold的返回值范例则不受束缚。通过 reduce 实现元素求和:
val numbers = listOf(2, 5, 1)val result = numbers.reduce { a: Int, b: Int -> a + b }println("reduceResult=$result")输出结果:
reduceResult=8通过 fold 将聚集拼接成字符串:
val numbers = listOf(2, 5, 1)val result = numbers.fold(StringBuilder()) { str: StringBuilder, i: Int -> str.append(i).append(" ")}println("foldResult=$result")输出结果:
foldResult=2 5 1 ---------------------------fold : 合拢,折叠reduce : 压缩两个函数都是对聚集的遍历,只是遍历完成之后能得到一个结果。折叠和压缩的意思,可以明确为,将聚集折叠成一个新的对象【对象的范例,可以与聚集元素的范例无关】大概压缩成一个值【范例必须和聚集元素的范例划一】。八、取聚集中的一部门
【1】利用 Slice 返回具有给定索引的聚集元素列表
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.slice(1..3))println(numbers.slice(0..4 step 2))println(numbers.slice(setOf(3, 5, 0)))输出结果:
[two, three, four][one, three, five][four, six, one]【2】利用Take 与 drop
take:从头开始获取指定命量的元素
drop:从头开始去除指定命量的元素
takeLast:从尾开始获取指定命量的元素
dropLast:从尾开始去除指定命量的元素
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.take(3))println(numbers.takeLast(3))println(numbers.drop(1))println(numbers.dropLast(5))输出结果:
[one, two, three][four, five, six][two, three, four, five, six][one]利用谓词来界说要获取或去除的元素的数目(takeWhile 、takeLastWhile、dropWhile、dropLastWhile):
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.takeWhile { !it.startsWith('f') })println(numbers.takeLastWhile { it != "three" })println(numbers.dropWhile { it.length == 3 })println(numbers.dropLastWhile { it.contains('i') })输出结果:
[one, two, three][four, five, six][three, four, five, six][one, two, three, four]【3】利用 chunked 将聚集分解成给定巨细的"块"
val numbers = (0..13).toList()println(numbers.chunked(3))输出结果:
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13]]还可以返回块的转换:
val numbers = (0..13).toList() println(numbers.chunked(3) { it.sum() }) // `it` 为原始聚集的一个块输出结果:
[3, 12, 21, 30, 25]【4】利用 Windowed 检索给定巨细的聚集元素中全部大概区间
val numbers = listOf("one", "two", "three", "four", "five") println(numbers.windowed(3))输出结果:
[[one, two, three], [two, three, four], [three, four, five]]windowed()有两个参数:step 、partialWindows
step:隔断
partialWindows:是否包罗较小的窗口
val numbers = (1..10).toList()println(numbers.windowed(3, step = 2, partialWindows = true))println(numbers.windowed(3, step = 2, partialWindows = false))输出结果:
[[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]][[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]还可以返回块的转换:
val numbers = (1..10).toList()println(numbers.windowed(3) { it.sum() })输出结果:
[6, 9, 12, 15, 18, 21, 24, 27]【5】利用 zipWithNext 创建汲取器聚集的相邻元素对
val numbers = listOf("one", "two", "three", "four", "five") println(numbers.zipWithNext())println(numbers.zipWithNext() { s1, s2 -> s1.length > s2.length})输出结果:
[(one, two), (two, three), (three, four), (four, five)][false, false, true, false]九、取单个元素
【1】按位置取
val numbers = linkedSetOf("one", "two", "three", "four", "five")println(numbers.elementAt(3)) // 相称于numbers[3]输出结果:
four排序或获取单个元素:
val numbersSortedSet = sortedSetOf("one", "two", "three", "four")println(numbersSortedSet) println(numbersSortedSet.elementAt(0)) // 相称于numbers[0]输出结果:
[four, one, three, two]four利用 first 和 last 获取单个元素:
val numbers = listOf("one", "two", "three", "four", "five")println(numbers.first()) println(numbers.last()) 输出结果:
onefive具有安全性子获取单个元素(防止空指针和角标越界引起的瓦解):
val numbers = listOf("one", "two", "three", "four", "five")println(numbers.elementAtOrNull(5)) // 相称于 numbers.getOrNull(5)println(numbers.elementAtOrElse(5) { index -> "The value for index $index is undefined"}) // 相称于 numbers.getOrElse()输出结果:
nullThe value for index 5 is undefined【2】按条件取
利用 first 、last 按条件获取:
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.first { it.length > 3 })println(numbers.last { it.startsWith("f") })输出结果:
threefive为了制止空指针导致的非常,可以利用 firstOrNull、 lastOrNull 代替:
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.firstOrNull { it.length > 6 })println(numbers.lastOrNull { it.length > 6 })输出结果:
nullnull利用 find 和 findLast 按条件获取:
val numbers = listOf(1, 2, 3, 4)println(numbers.find { it % 2 == 0 })println(numbers.findLast { it % 2 == 0 })输出结果:
24【3】随机取元素
val numbers = listOf(1, 2, 3, 4)println(numbers.random())【4】检测存在与否
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.contains("four"))println("zero" in numbers)println(numbers.containsAll(listOf("four", "two")))println(numbers.containsAll(listOf("one", "zero")))输出结果:
truefalsetruefalse【5】查抄聚集中是否为空或非空
val numbers = listOf("one", "two", "three", "four", "five", "six")println(numbers.isEmpty())println(numbers.isNotEmpty())val empty = emptyList<String>()println(empty.isEmpty())println(empty.isNotEmpty())输出结果:
falsetruetruefalse十、排序
【1】实现 Comparable 接口自界说比力规则,并排序:
class Version(val major: Int, val minor: Int): Comparable<Version> { override fun compareTo(other: Version): Int { if (this.major != other.major) { return this.major - other.major } else if (this.minor != other.minor) { return this.minor - other.minor } else return 0 } override fun toString(): String { return "($major,$minor)" }}var list = arrayListOf(Version(1, 2), Version(1, 3), Version(2, 0), Version(1, 5))list.sort()println(list)输出结果:
[(1,2), (1,3), (1,5), (2,0)]【2】还可以利用 Comparator 对象排序:
val versionComparator = Comparator<Version> { version1: Version, version2: Version -> if (version1.major != version2.major) { version1.major - version2.major } else if (version1.minor != version2.minor) { version1.minor - version2.minor } else 0}var list = arrayListOf(Version(1, 2), Version(1, 3), Version(2, 0), Version(1, 5))list.sortWith(versionComparator)println(list)输出结果:
[(1,2), (1,3), (1,5), (2,0)]【3】天然排序
sorted(): 升序排序
sortedDescending():降序排序
val numbers = listOf("one", "two", "three", "four")println("Sorted ascending: ${numbers.sorted()}") println("Sorted descending: ${numbers.sortedDescending()}")输出结果:
Sorted ascending: [four, one, three, two]Sorted descending: [two, three, one, four]聚集的元素都是字符串,字符串的天然次序是比力字符串的巨细。
【4】利用 sortedBy 自界说一个天然次序
val numbers = listOf("one", "two", "three", "four")val sortedNumbers = numbers.sortedBy { it.length }println("Sorted by length ascending: $sortedNumbers")val sortedByLast = numbers.sortedByDescending { it.last() }println("Sorted by the last letter descending: $sortedByLast")输出结果:
Sorted by length ascending: [one, two, four, three]Sorted by the last letter descending: [four, two, one, three]【5】倒序排序
val numbers = listOf("one", "two", "three", "four")println(numbers.reversed())输出结果:
[four, three, two, one]【6】随机次序
val numbers = listOf("one", "two", "three", "four")println(numbers.shuffled())十一、聚集聚合利用
聚集聚合利用:基于聚集内容返回单个值的利用
利用 count、max、min、average、sum函数:
val numbers = listOf(6, 42, 10, 4)println("Count: ${numbers.count()}")println("Max: ${numbers.max()}")println("Min: ${numbers.min()}")println("Average: ${numbers.average()}")println("Sum: ${numbers.sum()}")输出结果:
Count: 4Max: 42Min: 4Average: 15.5Sum: 62利用 minBy 、maxBy 、maxWith、minWith:
val numbers = listOf(5, 42, 10, 4)val minRemainder = numbers.minBy { it % 3 }println(minRemainder)val maxRemainder = numbers.maxBy { it % 3 }println(maxRemainder)val strings = listOf("one", "two", "three", "four")val maxLengthString = strings.maxWith(compareBy { it.length })println(maxLengthString)val minLengthString = strings.minWith(compareBy { it.length })println(minLengthString)输出结果:
425threeone利用高级求和函数sumBy 、sumByDouble:
val numbers = listOf(5, 42, 10, 4)println(numbers.sumBy { it * 2 })println(numbers.sumByDouble { it.toDouble() / 2 })输出结果:
12230.5十二、聚集写利用
【1】添加元素
add:
val numbers = mutableListOf(1, 2, 3, 4)numbers.add(5)println(numbers)输出结果:
[1, 2, 3, 4, 5]addAll:
val numbers = mutableListOf(1, 2, 5, 6)numbers.addAll(arrayOf(7, 8))println(numbers)numbers.addAll(2, setOf(3, 4))println(numbers)输出结果:
[1, 2, 5, 6, 7, 8][1, 2, 3, 4, 5, 6, 7, 8]利用 plus 运算符 (-) 和 plusAssign (+=) 添加元素:
val numbers = mutableListOf("one", "two")numbers += "three"println(numbers)numbers += listOf("four", "five") println(numbers)输出结果:
[one, two, three][one, two, three, four, five]【2】删除元素
val numbers = mutableListOf(1, 2, 3, 4, 3)numbers.remove(3) // 删除numbers.retainAll { it >= 3 } // 保存numbers.clear() // 清空val numbersSet = mutableSetOf("one", "two", "three", "four")numbersSet.removeAll(setOf("one", "two")) // 删除全部十三、List干系利用
【1】按索引取元素
val numbers = listOf(1, 2, 3, 4)println(numbers.get(0))println(numbers[0])println(numbers.getOrNull(5)) println(numbers.getOrElse(5, {it})) 【2】取列表的一部门
val numbers = (0..13).toList()println(numbers.subList(3, 6))输出结果:
[3, 4, 5]【3】查找元素位置
线性查找:
val numbers = listOf(1, 2, 3, 4, 2, 5)println(numbers.indexOf(2))println(numbers.lastIndexOf(2))val numbers = mutableListOf(1, 2, 3, 4)println(numbers.indexOfFirst { it > 2})println(numbers.indexOfLast { it % 2 == 1})在有序列表中二分查找:
val numbers = mutableListOf("one", "two", "three", "four")numbers.sort()println(numbers)println(numbers.binarySearch("two")) println(numbers.binarySearch("z")) println(numbers.binarySearch("two", 0, 2)) Comparator 二分搜刮:
data class Product(val name: String, val price: Double)fun main() { val productList = listOf( Product("WebStorm", 49.0), Product("AppCode", 99.0), Product("DotTrace", 129.0), Product("ReSharper", 149.0)) println(productList.binarySearch(Product("AppCode", 99.0), compareBy<roduct> { it.price }.thenBy { it.name }))}利用 String.CASE_INSENSITIVE_ORDER:
val colors = listOf("Blue", "green", "ORANGE", "Red", "yellow")println(colors.binarySearch("RED", String.CASE_INSENSITIVE_ORDER)) 比力函数二分搜刮:
data class Product(val name: String, val price: Double)fun priceComparison(product: Product, price: Double) = sign(product.price - price).toInt()fun main() { val productList = listOf( Product("WebStorm", 49.0), Product("AppCode", 99.0), Product("DotTrace", 129.0), Product("ReSharper", 149.0)) println(productList.binarySearch { priceComparison(it, 99.0) })}【4】添加
val numbers = mutableListOf("one", "five", "six")numbers.add(1, "two")numbers.addAll(2, listOf("three", "four"))println(numbers)【5】更新
val numbers = mutableListOf("one", "five", "three")numbers[1] = "two"println(numbers)【6】更换
val numbers = mutableListOf(1, 2, 3, 4)numbers.fill(3) // 将全部元素更换成指定值println(numbers)输出结果:
[3, 3, 3, 3]【7】删除
val numbers = mutableListOf(1, 2, 3, 4, 3) numbers.removeAt(1)println(numbers)val numbers = mutableListOf(1, 2, 3, 4, 3) numbers.removeFirst()numbers.removeLast()println(numbers)val empty = mutableListOf<Int>()// empty.removeFirst() // NoSuchElementException: List is empty.empty.removeFirstOrNull() //null【8】排序
val numbers = mutableListOf("one", "two", "three", "four")numbers.sort()println("Sort into ascending: $numbers")numbers.sortDescending()println("Sort into descending: $numbers")numbers.sortBy { it.length }println("Sort into ascending by length: $numbers")numbers.sortByDescending { it.last() }println("Sort into descending by the last letter: $numbers")numbers.sortWith(compareBy<String> { it.length }.thenBy { it })println("Sort by Comparator: $numbers")numbers.shuffle() // 随机println("Shuffle: $numbers")numbers.reverse() // 反转println("Reverse: $numbers")输出结果:
Sort into ascending: [four, one, three, two]Sort into descending: [two, three, one, four]Sort into ascending by length: [two, one, four, three]Sort into descending by the last letter: [four, two, one, three]Sort by Comparator: [one, two, four, three]Shuffle: [two, one, three, four]Reverse: [four, three, one, two]十四、Set干系利用
val numbers = setOf("one", "two", "three")println(numbers union setOf("four", "five")) // 并集println(setOf("four", "five") union numbers) // 并集println(numbers intersect setOf("two", "one")) // 交集println(numbers subtract setOf("three", "four")) // 差集println(numbers subtract setOf("four", "three")) // 差集输出结果:
[one, two, three, four, five][four, five, one, two, three][one, two][one, two][one, two]十五、Map干系利用
【1】取键与值
val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)println(numbersMap.get("one"))println(numbersMap["one"])numbersMap.getOrElse("four") {"错误"}println(numbersMap.getOrDefault("four", 10))val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)println(numbersMap.keys)println(numbersMap.values)【2】过滤
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10}println(filteredMap)val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)val filteredKeysMap = numbersMap.filterKeys { it.endsWith("1") }val filteredValuesMap = numbersMap.filterValues { it < 10 }println(filteredKeysMap)println(filteredValuesMap)【3】plus 与 minus 利用
val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)println(numbersMap + Pair("four", 4))println(numbersMap + Pair("one", 10))println(numbersMap + mapOf("five" to 5, "one" to 11))val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)println(numbersMap - "one")println(numbersMap - listOf("two", "four"))【4】添加与更新
val numbersMap = mutableMapOf("one" to 1, "two" to 2)numbersMap.put("three", 3)println(numbersMap)val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)numbersMap.putAll(setOf("four" to 4, "five" to 5))println(numbersMap)【5】plusAssign(+=) 利用符
val numbersMap = mutableMapOf("one" to 1, "two" to 2)numbersMap["three"] = 3 // 调用 numbersMap.set("three", 3)numbersMap += mapOf("four" to 4, "five" to 5)println(numbersMap)【6】删除
val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)numbersMap.remove("one")println(numbersMap)numbersMap.remove("three", 4) //不会删除任何条目println(numbersMap)val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "threeAgain" to 3)numbersMap.keys.remove("one")println(numbersMap)numbersMap.values.remove(3)println(numbersMap)利用 minusAssign(-=) 利用符:
val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)numbersMap -= "two"println(numbersMap)numbersMap -= "five" //不会删除任何条目println(numbersMap)[本章完...] |