javaru
08/21/2019, 3:08 PMStream<T>
to a typed array of Array<T>
?
I need to convert a Map<String,String>
to an Array<Pair<String,String>>
. I've done this as follows:
val array: Array<Pair<String, String>> = myMap.entries.stream().map {
Pair(it.key, it.value)
}.toList().toTypedArray()
Is that the most efficient way? It seems like converting to a List
first and then to the (typed) Array
has an extra step. But I'm not seeing any other way to go from the Stream<Pair<String,String>>
coming out of the map{}
function to a typed Array<Pair<String,String>>
(since Stream.toArray()
would give me an untyped Array of Array<Any>
).ilya.gorbunov
08/21/2019, 3:11 PMstream
here at all: use map.entries.toTypedArray()
javaru
08/21/2019, 3:13 PMilya.gorbunov
08/21/2019, 3:16 PMval entryIterator = map.entries.iterator()
val pairs = Array(map.size) { entryIterator.next().toPair() }
would be most efficient way.
Not very fluent, though, and not recommended to use with concurrent maps.javaru
08/21/2019, 3:17 PMPair
(as I'm calling a function from a library that takes a vararg Pair<String, String>
)