Kotlin: How to convert list to map with list? - kotlin

I have a list as below
{("a", 1), ("b", 2), ("c", 3), ("a", 4)}
I want to convert it to a map of list as below
{("a" (1, 4)), ("b", (2)), ("c", (3)))}
i.e. for a, we have a list of 1 and 4, since the key is the same.
The answer in
How to convert List to Map in Kotlin? only show unique value (instead of duplicate one like mine).
I tried associateBy in Kotlin
data class Combine(val alpha: String, val num: Int)
val list = arrayListOf(Combine("a", 1), Combine("b", 2), Combine("c", 3), Combine("a", 4))
val mapOfList = list.associateBy ( {it.alpha}, {it.num} )
println(mapOfList)
But doesn't seems to work. How could I do it in Kotlin?

Code
fun main(args: Array<String>) {
data class Combine(val alpha: String, val num: Int)
val list = arrayListOf(Combine("a", 1), Combine("b", 2), Combine("c", 3), Combine("a", 4))
val mapOfList = list.associateBy ( {it.alpha}, {it.num} )
println(mapOfList)
val changed = list
.groupBy ({ it.alpha }, {it.num})
println(changed)
}
Output
{a=4, b=2, c=3}
{a=[1, 4], b=[2], c=[3]}
How it works
First it takes the list
It groups the Combines by their alpha value to their num values

You may group the list by alpha first and then map the value to List<Int>:
data class Combine(val alpha: String, val num: Int)
val list = arrayListOf(Combine("a", 1), Combine("b", 2), Combine("c", 3), Combine("a", 4))
val mapOfList = list
.groupBy { it.alpha }
.mapValues { it.value.map { it.num } }
println(mapOfList)

Here's a slightly more concise version of Jacky Choi's solution.
It combines the grouping and the transforming into one call to groupBy().
val mapOfList = list
.groupBy (
keySelector = { it.name },
valueTransform = { it.num },
)

Related

Divide list into two list

Is there a simple way to divide list of Double into two lists of pairs in Kotlin?
In such way:
[x1, y1, x2, y2, x3, y3] => [(x1, x2), (x2, x3), (x3, x1)], [(y1, y2), (y2, y3), (y3, y1)]
I tried to use filterIndexed and zipWithNext
val x = filterIndexed { index, _ -> index % 2 == 0 }.zipWithNext()
val y = filterIndexed { index, _ -> index % 2 == 1 }.zipWithNext()
But the result is:
[x1, y1, x2, y2, x3, y3] => [(x1, x2), (x2, x3)], [(y1, y2), (y2, y3)]
If I understand correctly, the problem with the zipWithNext that you are using is that it doesn't "wrap around", i.e. output the final (x3, x1) or (y3, y1) pair, containing the last and first elements of the list.
You can fix this by simply declaring your own version of zipWithNext that does do this.
You can either do something like this:
fun <T> Iterable<T>.zipWithNextAndWrapAround(): List<Pair<T, T>> {
val zippedWithNext = zipWithNext()
if (zippedWithNext.isEmpty()) return zippedWithNext
return zippedWithNext + (zippedWithNext.last().second to zippedWithNext.first().first)
}
Or copy and paste over the original source code of zipWithNext and slightly modify it:
fun <T> Iterable<T>.zipWithNextAndWrapAround(): List<Pair<T, T>> {
val iterator = iterator()
if (!iterator.hasNext()) return emptyList()
val result = mutableListOf<Pair<T, T>>()
var current = iterator.next()
// remember what the first element was
val first = current
while (iterator.hasNext()) {
val next = iterator.next()
result.add(current to next)
current = next
}
// at last, add this pair
result.add(current to first)
return result
}
Usage:
val x = list.filterIndexed { index, _ -> index % 2 == 0 }.zipWithNextAndWrapAround()
val y = list.filterIndexed { index, _ -> index % 2 == 1 }.zipWithNextAndWrapAround()
Note that this is looping through the list twice. You can avoid that by writing your own version of partition called partitionIndexed.
The code could be something like:
inline fun <T> Iterable<T>.partitionIndexed(predicate: (Int, T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
forEachIndexed { index, element ->
if (predicate(index, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}
// usage:
val (x, y) = list.partitionIndexed { index, _ ->
index % 2 == 0
}.let { (a, b) ->
a.zipWithNextAndWrapAround() to b.zipWithNextAndWrapAround()
}
You could do something like this:
val lst = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val intermediate = lst.chunked(2).map { it[0] to it[1] }.let { it + it[0] }
val x = intermediate.map { it.first }.zipWithNext()
val y = intermediate.map { it.second }.zipWithNext()
println(x) //[(1, 3), (3, 5), (5, 7), (7, 1)]
println(y) //[(2, 4), (4, 6), (6, 8), (8, 2)]
val input = listOf("x1", "y1", "x2", "y2", "x3", "y3")
val result = list
.withIndex()
.groupBy { it.index % 2 }
.map { entry -> entry.value.map { it.value } }
.map { (it + it[0]).zipWithNext() }
println(result)
Output:
[[(x1, x2), (x2, x3), (x3, x1)], [(y1, y2), (y2, y3), (y3, y1)]]

How can I get the different elements for two List with Kotlin?

I hope to get the different elements in two list, how can I do it with Kotlin?
Such as , the different elements is 9 and 10 in Code A.
Code A
val mutableList1 = mutableListOf(1,2,7,8)
val mutableList2 = mutableListOf(1,2,7,8,9,10)
You could use filterNot:
fun main() {
val mutableList1 = mutableListOf(1, 2, 7, 8)
val mutableList2 = mutableListOf(1, 2, 7, 8, 9, 10)
val differentElements = mutableList2.filterNot { mutableList1.contains(it) }
println(differentElements)
}
Output:
[9, 10]

Split a list into three in Kotlin

I have the following data class:
data class Foo(val a: Int = 0, val b: Int = 0)
I have a list of Foo's with the following structure:
[ Foo(a = 1), Foo(a = 2), ..., Foo(b = 22), Foo(a = 5), Foo(a = 6), ... ]
(a group of items with a's, then one b, then a's again)
I would like to split above list into three sub-lists like that:
[ Foo(a = 1), Foo(a = 2), ...]
[ Foo(b = 22) ]
[ Foo(a = 5), Foo(a = 6), ...]
sublist of elements that have non-zero a property
list of one element that has non-zero b
remaining sublist of elements that have non-zero a property
Is it possible to achieve using groupBy or partition?
It is not possible to do it via groupBy or partition, because it is not possible to check the past state in those operations. However, you can do it via a fold operation and using mutable lists. Not sure if it fits to your needs but here it goes:
val input = listOf(Foo(a = 1), Foo(a = 2), Foo(b = 22), Foo(a = 5), Foo(a = 6))
val output: List<List<Foo>> = input.fold(mutableListOf<MutableList<Foo>>(mutableListOf())) { acc, foo ->
val lastList = acc.last()
val appendToTheLastList =
lastList.isEmpty() ||
(foo.a != 0 && lastList.last().a != 0) ||
(foo.b != 0 && lastList.last().b != 0)
when {
appendToTheLastList -> lastList.add(foo)
else -> acc.add(mutableListOf(foo))
}
return#fold acc
}
println(output)
outputs:
[[Foo(a=1, b=0), Foo(a=2, b=0)], [Foo(a=0, b=22)], [Foo(a=5, b=0),
Foo(a=6, b=0)]]
Note: I have to point out that this solution is not better than a solution with regular loops.
So you want to 1) ignore the first Foos where a=0, 2) start collecting them when you see Foos where a is non-zero, 3) when you hit a Foo where a=0, put that in another list, because b will be non-zero, 4) start collecting non-zero a's again in a third list?
If that's what you want (this is an extremely specific thing you want and you haven't been clear about it at all) you could do it this way:
data class Foo(val a: Int, val b: Int)
val stuff = listOf(Foo(0,1), Foo(1,2), Foo(3,0), Foo(0, 4), Foo(0, 5), Foo(6, 1), Foo(0,7))
fun main(args: Array<String>) {
fun aIsZero(foo: Foo) = foo.a == 0
// ignore initial zero a's if there are any
with(stuff.dropWhile(::aIsZero)) {
val bIndex = indexOfFirst(::aIsZero)
val listOne = take(bIndex)
val listTwo = listOf(elementAt(bIndex))
val listThree = drop(bIndex+1).filterNot(::aIsZero)
listOf(listOne, listTwo, listThree).forEach(::println)
}
}
You can't use partition or groupBy because your predicate depends on the value of a, but also on whether it happens to represent that one element you want to put in the b list, and for the others whether they appear before or after that b element. Which you don't know before you start processing the list.
You could mess around with indices and stuff, but honestly your use case seems so specific that it's probably better to just do it imperatively instead of trying to cram it into a functional approach.

Is there any operation to multiply each element in the first array to each element in the second array in Kotlin?

Is there any function (like fold, map, filter), which gets 2 arrays and lambda-function (for example multiplication) as parameters and returns third array?
I've used cycle for, but is there more beautiful method?
Yes, there is zip (nice example at the bottom of the page), see this (different) example:
fun main() {
val a = arrayOf( 1, 2, 3, 4 )
val b = arrayOf( 1, 2, 3, 4 )
val c = a.zip(b) { i, j -> i * j }
println(c)
}
which outputs
[1, 4, 9, 16]
There isn't a built in specifically but you can do this:
array1.zip(array2).map { (x,y) -> x*y }

Product of a List or Array in Kotlin

I'm trying to find a way to get the product of a List or Array without using "repeat" or any loop on Kotlin but after some research I couldn't find anything similar.
Something like this in Python would be:
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
output: 720
You can use reduce in Kotlin.
From the doc:
Accumulates value starting with the first element and applying
operation from left to right to current accumulator value and each
element.
val list = listOf<Int>(1, 2, 3, 4, 5, 6)
val array = intArrayOf(1, 2, 3, 4, 5, 6)
list.reduce { acc, i -> acc * i } // returns 720
array.reduce { acc, i -> acc * i } // returns 720
An even simpler solution might be: (1..6).reduce(Int::times)
Use the fold function
val total = listOf(1, 2, 3, 4, 5).fold(1, { total, next -> total * next })
Hope this helps:
fun main(args: Array<String>){
val array = intArrayOf(1, 2, 3, 4, 5, 6)
val product = array.fold(1){acc, i -> acc * i}
println("The result is: $product")
}
This will output the product of the array.
Use the fold or reduce function. Both will work.
val array = arrayOf(1, 2, 3, 4, 5, 6)
println("Product of list: ${listOfMultiplication(array)}")
fun listOfMultiplication(array: Array<Int>): Int {
return array.reduce { total, next -> total * next }
}
val array = arrayOf(1, 2, 3, 4, 5, 6)
println("Product of list: ${listOfMultiplication(array)}")
fun listOfMultiplication(array: Array<Int>): Int {
return array.fold(1) { total, next -> total * next }
}