1 前言
序列(Sequence)是 Kotlin 中为方便操作集合及其元素而定制的接口,是一个延迟获取数据的集合,只有需要元素时才会生产元素。在处理大量数据时,序列可以显著地提升性能。
Sequence 类似 Java 中的 Stream,详见 → Stream 总结。Sequence 有中间操作和终端操作,如下。
- 中间操作:每次操作返回一个新的 Sequence 对象(主要操作有:filter、distinct、drop、take、sorted、sortedDescending、map、mapIndexed、minus 等)。
- 终端操作:每次操作返回一个值或集合,每个序列只能进行一次终端操作(主要操作有:forEach、joinToString、min、max、count、sum、average、any、first、last、reduce 等)。
2 Sequence 的创建
2.1 sequenceOf
var sequence = sequenceOf(1, 2, 3)
2.2 asSequence
1)IntRange
var intRange = 1..10
var sequence = intRange.asSequence()
2)Array
var array = arrayOf(1, 2, 3)
var sequence = array.asSequence()
3)List
var list = listOf("AA", "BBB", "CC")
var sequence = list.asSequence()
4)Set
var set = setOf("AA", "BBB", "CC")
var sequence = set.asSequence()
2.3 BufferedReader
val reader = BufferedReader(FileReader("G:\\stream.txt"))
val sequence = reader.lineSequence()
2.4 generateSequence
var sequence = generateSequence(arrayOf(1, 1)) { arr ->
arrayOf(arr[1], arr[0] + arr[1])
}
.take(10)
.map { arr -> arr[0] }
// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
println(sequence.joinToString())
3 Sequence 的中间操作
3.1 主要接口
// 过滤
public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T>
// 去重
public fun <T> Sequence<T>.distinct(): Sequence<T>
// 丢弃前 n 个元素
public fun <T> Sequence<T>.drop(n: Int): Sequence<T>
// 截取前 n 个元素
public fun <T> Sequence<T>.take(n: Int): Sequence<T>
// 排序(升序)
public fun <T : Comparable<T>> Sequence<T>.sorted(): Sequence<T>
// 排序(降序)
public fun <T : Comparable<T>> Sequence<T>.sortedDescending(): Sequence<T>
// 映射(T -> R)
public fun <T, R> Sequence<T>.map(transform: (T) -> R): Sequence<R>
// 映射(index, T -> R)
public fun <T, R> Sequence<T>.mapIndexed(transform: (index: Int, T) -> R): Sequence<R>
// 删除序列中第一个 element
public operator fun <T> Sequence<T>.minus(element: T): Sequence<T>
3.2 案例
fun main() {
var sequence = sequenceOf(4, 9, 1, 8, 5, 5, 7, 3, 6, 2)
sequence.filter { e -> e > 2 && e < 8 } // 4 5 5 7 3 6
.distinct() // 4 5 7 3 6
.drop(1) // 5 7 3 6
.take(3) // 5 7 3
.sorted() // 3 5 7
.map { e -> e * e } // 9 25 49
.forEach(::println)
}
4 Sequence 的终端操作
4.1 主要接口
1)统计函数
// 最小值
public fun <T : Comparable<T>> Sequence<T>.min(): T?
// 最大值
public fun <T : Comparable<T>> Sequence<T>.max(): T?
// 元素个数
public fun <T> Sequence<T>.count(): Int
// 求和
public fun Sequence<Int>.sum(): Int
// 求平均值
public fun Sequence<Int>.average(): Double
// 序列中是否有元素
public fun <T> Sequence<T>.any(): Boolean
// 获取第一个元素
public fun <T> Sequence<T>.first(): T
// 获取最后一个元素
public fun <T> Sequence<T>.last(): T
2)遍历元素
// 遍历元素
public inline fun <T> Sequence<T>.forEach(action: (T) -> Unit): Unit
3)拼接元素
// 转换为字符串
public fun <T> Sequence<T>.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String
4)规约运算
// 规约运算,定义运算 o, result = ((((e1 o e2)) o e3) o e4) o ...
public inline fun <S, T : S> Sequence<T>.reduce(operation: (acc: S, T) -> S): S
5)集合转换
public fun <T> Sequence<T>.toList(): List<T>
public fun <T> Sequence<T>.toMutableList(): MutableList<T>
public fun <T> Sequence<T>.toSet(): Set<T>
public fun <T> Sequence<T>.toMutableSet(): MutableSet<T>
public fun <T> Sequence<T>.toHashSet(): HashSet<T>
public fun <T : Comparable<T>> Sequence<T>.toSortedSet(): java.util.SortedSet<T>
4.2 案例
1)统计函数
fun main() {
var sequence = sequenceOf(1, 3, 5)
var min = sequence.min() // 1
var max = sequence.max() // 5
var count = sequence.count() // 3
var sum = sequence.sum() // 9
var avg = sequence.average() // 3
var hasElement = sequence.any() // true
var first = sequence.first() // 1
var last = sequence.last() // 5
}
2)遍历元素
fun main() {
var sequence = sequenceOf(1, 3, 5)
sequence.forEach(::println) // 1、3、5
}
3)拼接元素
fun main() {
var sequence = sequenceOf(1, 3, 5)
var str = sequence.joinToString(",", "[", "]")
}
4)规约运算
fun main() {
var sequence = sequenceOf(1, 3, 5)
var sum = sequence.reduce(Integer::sum) // 9
// 1*1-3*3=-8, (-8)*(-8)-5*5=39
var res = sequence.reduce { e1, e2 -> e1 * e1 - e2 * e2 } // 39
}
5)集合转换
fun main() {
val sequence = sequenceOf(1, 2, 3)
var list = sequence.toList()
var mutableList = sequence.toMutableList()
var set = sequence.toSet()
var mutableSet = sequence.toMutableSet()
var hashSet = sequence.toHashSet()
var sortedSet = sequence.toSortedSet()
}
声明:本文转自【Kotlin】Sequence简介。
标签:元素,sequence,Kotlin,Sequence,var,简介,fun,public From: https://www.cnblogs.com/zhyan8/p/18115109