Kotlin学习笔记(四) 集合(下)

集合

十一、取集合的一部分
  • Slice

slice()返回具有给定索引的集合元素列表。

val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.slice(1..3))  // [two, three, four]
println(numbers.slice(0..4 step 2))  // [one, three, five]
println(numbers.slice(listOf(3, 5, 0)))  // [four, six, one]
  • Take 与 Drop

take()从头获取指定数量的元素,takeLast()从尾开始获取。

drop()从头去除指定数量的元素,dropLast()从尾开始去除。

val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.take(3))  // [one, two, three]
println(numbers.takeLast(3))  // [four, five, six]
println(numbers.drop(4))  // [five, six]
println(numbers.dropLast(5))  // [one]

takeWhile():不停获取元素直到排除与谓词匹配的首个元素。

takeLastWhile():从集合末尾获取与谓词匹配的元素区间。

dropWhile():返回从首个与谓词不匹配的元素到末尾元素。

dropLastWhile():返回从开头到最后一个与谓词不匹配的元素。

val numbers = listOf("one", "two", "three", "four", "five", "six")

println(numbers.takeWhile {
    !it.startsWith('f') })
// [one, two, three]

println(numbers.takeLastWhile {
    it != "three" })
// [four, five, six]

println(numbers.dropWhile {
    it.length == 3 })
// [three, four, five, six]

println(numbers.dropLastWhile {
    it.contains('i') })
// [one, two, three, four]
  • Chunked

chunked():将集合分解为给定大小的块。参数:块的大小;返回值:一个List其中包含给定大小的List

val numbers = (0..13).toList()

println(numbers.chunked(3))
// [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13]]

println(numbers.chunked(3) {
    it.sum() })
// [3, 12, 21, 30, 25]
  • Windowed

windowed():检索给定大小的集合元素中所有可能区间。

可选参数:

step两个相邻窗口的第一个元素之间的距离,默认为1

partialWindows是否包含小于给定大小的窗口,默认为false

val numbers = (1..10).toList()

println(numbers.windowed(3, step = 2, partialWindows = true))
// [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]]

println(numbers.windowed(3, step = 2))
// [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]

println(numbers.windowed(3, step = 2) {
    it.sum() })
// [6, 12, 18, 24]

构建两个元素的窗口:zipWithNext()

val numbers = listOf("one", "two", "three", "four", "five") 

println(numbers.zipWithNext())
// [(one, two), (two, three), (three, four), (four, five)]

println(numbers.zipWithNext {
    s1, s2 -> s1.length > s2.length})
// [false, false, true, false]
十二、取单个元素
  • 按位置取

elementAt()用一个整数作为参数。

val numbers = linkedSetOf("one", "two", "three", "four", "five")
println(numbers.elementAt(3))  // four

val numbersSortedSet = sortedSetOf("one", "two", "three", "four")
println(numbersSortedSet.elementAt(0))  // four

List可以使用get()[]

检索第一个和最后一个元素:first()last()

避免越界,使用elementAt()的安全变体:

elementAtOrNull():指定位置超出集合范围时,返回null

elementAtOrElse():接受一个lambda表达式。使用一个越界位置调用时,返回对给定值调用lambda表达式的结果。

val numbers = listOf("one", "two", "three", "four", "five")
println(numbers.elementAtOrNull(5))  // null
println(numbers.elementAtOrElse(5) {
    i -> "numbers[$i] is undefined"})  // numbers[5] is undefined
  • 按条件取

带有谓词的first():得到使调用谓词为true的第一个元素。

带有谓词的last():得到使调用谓词为true的最后一个元素。

安全调用:firstOrNull()lastOrNull()

find()可以代替firstOrNull()findLast()可以代替lastOrNull()

val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.first {
    it.length > 3 })        // three
println(numbers.last {
    it.startsWith("f") })    // five
println(numbers.firstOrNull {
    it.length > 5 })  // null
println(numbers.lastOrNull {
    it.length < 4 })   // six
println(numbers.find {
    it.length > 5 })         // null
println(numbers.findLast {
    it.length < 4 })     // six
  • 随机取
val numbers = listOf(1, 2, 3, 4)
println(numbers.random
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值