cy
01/19/2018, 5:24 PMval fullTariffList = arrayOf("customer", "reseller", "vendor").flatMap { type -> testTariffList.map { it.copy(type = type) } }
mike_shysh
01/19/2018, 5:27 PMfullTariffList.add(tariffInfo)
is always contains vendor
. I think I am missing some knowledge about forEach
r4zzz4k
01/19/2018, 5:30 PMmike_shysh
01/19/2018, 5:31 PMprintln(tariffInfo);
shows me that tariffInfo is different in every iterationfullTariffList.add(tariffInfo)
processed after finishing forEach?r4zzz4k
01/19/2018, 5:34 PMclass Object { var type: String }
val list = mutableListOf<Object>()
val obj = Object()
obj.type = "1"
println(obj.type) // "1"
list += obj // list has one reference to obj, it's type is "1"
obj.type = "2"
println(obj.type) // "2"
list += obj // list has two reference to the same obj, it's type is "2"
obj.type = "3"
println(obj.type) // "3"
list += obj // list has three reference to the same obj, it's type is "3"
list.forEach {
println(it.type)
}
// prints "3 3 3", as it's the same object and last value of type was "3"
.copy
allows you to create new object every iteration and list would have different objects inside it.mike_shysh
01/19/2018, 5:37 PMnkiesel
01/19/2018, 7:33 PMflatMap
solution creates a list with the same items but in a different order than the original code. Turning it inside out would fix this:
testTariffList.flatMap { item -> arrayOf("customer", "reseller", "vendor").map { item.copy(type = it) } }
mike_shysh
01/19/2018, 9:02 PM