stdlib: stream() for Map #KT-6547 Fixed
This commit is contained in:
@@ -119,6 +119,17 @@ public fun <T> Iterable<T>.stream(): Stream<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a stream from the given collection
|
||||||
|
*/
|
||||||
|
public fun <K, V> Map<K, V>.stream(): Stream<Map.Entry<K, V>> {
|
||||||
|
return object : Stream<Map.Entry<K, V>> {
|
||||||
|
override fun iterator(): Iterator<Map.Entry<K, V>> {
|
||||||
|
return this@stream.iterator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a stream from the given collection
|
* Returns a stream from the given collection
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -66,6 +66,12 @@ class MapTest {
|
|||||||
assertEquals("beverage,beer,location,Mells,name,James", list.join(","))
|
assertEquals("beverage,beer,location,Mells,name,James", list.join(","))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test fun stream() {
|
||||||
|
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
|
||||||
|
val named = map.stream().filter { it.key == "name" }.single()
|
||||||
|
assertEquals("James", named.value)
|
||||||
|
}
|
||||||
|
|
||||||
test fun iterateWithProperties() {
|
test fun iterateWithProperties() {
|
||||||
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
|
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
|
||||||
val list = arrayListOf<String>()
|
val list = arrayListOf<String>()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ fun streams(): List<GenericFunction> {
|
|||||||
val templates = arrayListOf<GenericFunction>()
|
val templates = arrayListOf<GenericFunction>()
|
||||||
|
|
||||||
templates add f("stream()") {
|
templates add f("stream()") {
|
||||||
|
include(Maps)
|
||||||
doc { "Returns a stream from the given collection" }
|
doc { "Returns a stream from the given collection" }
|
||||||
returns("Stream<T>")
|
returns("Stream<T>")
|
||||||
body {
|
body {
|
||||||
|
|||||||
Reference in New Issue
Block a user