java - Change a list to map in Kotlin while customize this convert -
var listofnums = listof(1,9,8,25,5,44,7,95,9,10) var mapofnums = listofnums.map { it+1 }.tomap() println(mapofnums)
result
{1=2, 9=10, 8=9, 25=26, 5=6, 44=45, 7=8, 95=96, 10=11}
while need result, adds contents of next element current element while need map current element next element
my goal result
{1=9, 8=25, 5=44, 7=59, 9=10}
for kotlin 1.1:
first, use zip create list adjacent pairs. drop every other pair, before converting map
. this:
val list = listof(1,9,8,25,5,44,7,95,9,10) val mapofnums = list.zip(list.drop(1)) .filterindexed { index, pair -> index % 2 == 0 } .tomap())
for kotlin 1.2:
kotlin 1.2 brings chunked
function make bit easier. function divides list in sublists of given length. can this:
val list = listof(1,9,8,25,5,44,7,95,9,10) val mapofnums = list.chunked(2).associate { (a, b) -> b }
Comments
Post a Comment