总数操作符
any
如果至少有一个元素符合给出的判断条件,则返回true。
1. val list = listOf(1, 2, 3, 4, 5, 6)
2. assertTrue(list.any { it % 2 == 0 })
3. assertFalse(list.any { it > 10 })
all
如果全部的元素符合给出的判断条件,则返回true。
1. assertTrue(list.all { it < 10 })
2. assertFalse(list.all { it % 2 == 0 })
count
返回符合给出判断条件的元素总数。
1. assertEquals(3, list.count { it % 2 == 0 })
fold
在一个初始值的基础上从第一项到最后一项通过一个函数累计所有的元素。
1. assertEquals(25, list.fold(4) { total, next -> total + next })
foldRight
与fold一样,但是顺序是从最后一项到第一项。
1. assertEquals(25, list.foldRight(4) { total, next -> total + next })
forEach
遍历所有元素,并执行给定的操作。
1. list.forEach { println(it) }
forEachIndexed
与forEach,但是我们同时可以得到元素的index。
1. list.forEachIndexed { index, value
2. -> println("position $index contains a $value") }
max
返回最大的一项,如果没有则返回null。
1. assertEquals(6, list.max())
maxBy
根据给定的函数返回最大的一项,如果没有则返回null。
1. // The element whose negative is greater
2. assertEquals(1, list.maxBy { -it })
min
返回最小的一项,如果没有则返回null。
1. assertEquals(1, list.min())
minBy
根据给定的函数返回最小的一项,如果没有则返回null。
1. // The element whose negative is smaller
2. assertEquals(6, list.minBy { -it })
none
如果没有任何元素与给定的函数匹配,则返回true。
1. // No elements are divisible by 7
2. assertTrue(list.none { it % 7 == 0 })
reduce
与fold一样,但是没有一个初始值。通过一个函数从第一项到最后一项进行累计。
1. assertEquals(21, list.reduce { total, next -> total + next })
reduceRight
与reduce一样,但是顺序是从最后一项到第一项。
1. assertEquals(21, list.reduceRight { total, next -> total + next })
sumBy
返回所有每一项通过函数转换之后的数据的总和。
1. assertEquals(3, list.sumBy { it % 2 })
