W3cubDocs

/Kotlin

asSequence

Platform and version requirements: JVM (1.0)
fun <T> Enumeration<T>.asSequence(): Sequence<T>

Creates a sequence that returns all values from this enumeration. The sequence is constrained to be iterated only once.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val numbers = java.util.Hashtable<String, Int>()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.put("three", 3)

// when you have an Enumeration from some old code
val enumeration: java.util.Enumeration<String> = numbers.keys()

// you can wrap it in a sequence and transform further with sequence operations
val sequence = enumeration.asSequence().sorted()
println(sequence.toList()) // [one, three, two]

// the resulting sequence is one-shot
// sequence.toList() //  will fail
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/java.util.-enumeration/as-sequence.html