Collection Transformations

Outline

  • Mapping

  • Zipping

  • Association

  • Flattening

  • String representation

Mapping

map and mapIndexed

val numbers = setOf(1, 2, 3)
println(numbers.map { it * 3 })
println(numbers.mapIndexed { idx, value -> value * idx })


output
[3, 6, 9]
[0, 2, 6]

mapNotNull and mapIndexedNotNull

val numbers = setOf(1, 2, 3)
println(numbers.mapNotNull { 
	if ( it == 2) null else it * 3 
})
println(numbers.mapIndexedNotNull { idx, value -> 
	if (idx == 0) null else value * idx 
})


output
[3, 9]
[2, 6]

mapKeys and mapValues

val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
println(numbersMap.mapKeys { it.key.toUpperCase() })
println(numbersMap.mapValues { it.value + it.key.length })


output
{KEY1=1, KEY2=2, KEY3=3, KEY11=11}
{key1=5, key2=6, key3=7, key11=16}

Zipping

a zip b

val colors = listOf("red", "brown", "gray")
val animals = listOf("fox", "bear", "wolf")
println(colors zip animals)


output
[(red, fox), (brown, bear), (gray, wolf)]

smaller size

val colors = listOf("red", "brown", "gray")
val twoAnimals = listOf("fox", "bear")
println(colors.zip(twoAnimals))


output
[(red, fox),(brown, bear)]

transformation

val colors = listOf("red", "brown", "gray")
val animals = listOf("fox", "bear", "wolf")
val result = colors.zip(animals){color, animal ->
  "The $animal is $ color"
}
println(result)


output
[fox is red, bear is brown, wolf is gray]

unzipping

val numberPairs = listOf("one" to 1, "two" to 2, "three" to 3)
println(numberPairs.unzip())


output
([one, two, three], [1, 2, 3])

Association

associateWith

val numbers = listOf("one", "two", "three", "four")
println(numbers.associateWith{ it.length})


output
{one=3, two=3, three=5, four=4}

associate

data class FullName (val firstName: String, val lastName: String)

fun parseFullName(fullName: String): FullName {
    val nameParts = fullName.split(" ")
    if (nameParts.size == 2) {
        return FullName(nameParts[0], nameParts[1])
    } else throw Exception("Wrong name format")
}

fun main(args: Array<String>) {
    val names = listOf("Alice Adams", 
    	"Brian Brown", "Clara Campbell")
    println(names.associate { name ->
        parseFullName(name).let { it.lastName to it.firstName }
    })
}


output
{Adams=Alice, Brown=Brian, Campbell=Clara}

Flattening

flatten

val numberSets = listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(1, 2))
println(numberSets.flatten())


output
[1, 2, 3, 4, 5, 6, 1, 2]

flatMap

val numberSets = listOf(listOf(1, 2, 3), 
	listOf(4, 5, 6), listOf(1, 2))
println(numberSets.flatMap { 
	it.map { value -> value * 10 } 
})


output
[10, 20, 30, 40, 50, 60, 10, 20]

joinToString

prefix、postfix

val numbers = listOf(1, 2, 3, 4)
println(names.joinToString(";","$","%"))


output
$1;2;3;4%

limittruncated

val numbers = (1..100).toList()
println(numbers.joinToString(limit = 10, truncated = "..."))

output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...

transform

val numbers = listOf(1, 2, 3, 4)
println(numbers.joinToString { "Element: $it" })

output
Element: 1, Element: 2, Element: 3, Element: 4

Thank you.

Collection Transformations

By givemepass

Collection Transformations

  • 629