Quincy
04/27/2021, 7:34 PMStream.of(...)
.toMap(
keySelector,
valueTransform,
(x, y) -> {
if (x.getTimestamp().isBefore(y.getTimestamp())) return x;
else return y;
});
If there would be duplicate values then the third argument determines what to do about it. In Kotlin, the associateBy function always keeps the most recent value. Is there a way to create a map the way Java does, where I get to decide what to do with duplicates?Nir
04/27/2021, 7:55 PMNir
04/27/2021, 7:56 PMmap basicallyRoukanken
04/27/2021, 8:01 PMlistOf(1, 2, 3, 4, 5)
.groupingBy { it % 2 }
.reduce { key, accumulator, element -> accumulator * element }
gets you
{1=15, 0=8}
(in reduce , accumulator is the value currently associated with key and element is the one that is trying to be new one)
there is also fold when you want to also do some type changing mapping, not just selecting or simple operations like I didQuincy
04/27/2021, 8:02 PMlistOf(...)
.groupingBy(keySelector)
.reduce { _, x, y -> if (x.timestamp.isBefore(y.timestamp)) x else y }Nir
04/27/2021, 8:05 PMNir
04/27/2021, 8:07 PM.associate followed by .mapValues, probably would be a little more readable arguablyNir
04/27/2021, 8:07 PMminNir
04/27/2021, 8:07 PMminBy ratherNir
04/27/2021, 8:08 PMQuincy
04/27/2021, 8:12 PM