dave08
01/04/2023, 4:05 PMdata class Foo(val id: String, ...)
and one of data class Baz(val id: String, ...)
and output a map of the id against a pair of Foo and Baz that correspond Map<String, Pair<Foo?, Baz?>>
where either could not exist?Landry Norris
01/04/2023, 4:07 PMdave08
01/04/2023, 4:09 PMSam
01/04/2023, 4:09 PMval foosById = foos.associateBy { it.id }
val bazsById = bazs.associateBy { it.id }
val pairs = (foosById.keys + bazsById.keys).associateWith { foosById[it] to bazsById[it] }
dave08
01/04/2023, 4:12 PMLandry Norris
01/04/2023, 4:13 PMbuildMap {
val list1 = listOf<Foo>()
val list2 = listOf<Bar>()
list1.forEach {
if(get(it.id) == null) put(it.id, it to null)
else put(it.id, it to get(it.id).second)
}
list2.forEach {
if(get(it.id) == null) put(it.id, null to it)
else put(it.id, get(it.id).first to it)
}
}
except take list1, list2 as params instead of creating them.Landry Norris
01/04/2023, 4:15 PMdave08
01/04/2023, 4:15 PMLandry Norris
01/04/2023, 4:16 PMLandry Norris
01/04/2023, 4:16 PMdave08
01/04/2023, 4:19 PMget(it.id) will return not null.It will go to the not null branch which adds it again...
by creating a mutable classwon't help because I need one pair for each combination to be saved in the final map, I just want to save on the intermediaries
Landry Norris
01/04/2023, 4:20 PMdave08
01/04/2023, 4:21 PMephemient
01/04/2023, 4:21 PMclass MutablePair<T, U>(var first: T, var second: U>
buildMap<String, MutablePair<Foo?, Bar?>> {
for (foo in foos) getOrPut(foo.id) { MutablePair(null, null) }.first = foo
for (bar in bars) getOrPut(bar.id) { MutablePair(null, null) }.second = bar
}
avoids extra temporaries, at a cost of exposed mutabilityLandry Norris
01/04/2023, 4:22 PMephemient
01/04/2023, 4:22 PMLandry Norris
01/04/2023, 4:25 PMdave08
01/04/2023, 4:27 PMFor large lists, O(n^2+2n) is less efficient than O(3n).So if it's only a few hundred entries per run, then O(n^2+2n), might be better? Or is that a lot?
ephemient
01/04/2023, 4:29 PMLandry Norris
01/04/2023, 4:29 PMephemient
01/04/2023, 4:29 PMLandry Norris
01/04/2023, 4:30 PMLandry Norris
01/04/2023, 4:31 PMephemient
01/04/2023, 4:33 PMdave08
01/04/2023, 4:35 PMLandry Norris
01/04/2023, 4:38 PMbuildMap {
val list1 = listOf<Foo>()
val list2 = listOf<Bar>()
list1.forEach { item1 ->
list2.forEach { item2 ->
if(item1.id == item2.id) put(item1.id, item1 to item2)
}
}
list1.forEach {
if(get(it.id) == null) put(it.id, it to null)
}
list2.forEach {
if(get(it.id) == null) put(it.id, null to it)
}
}
this solution has the downside of not handling duplicate ids in the same list well. How you sanitize the inputs will affect how much of a big deal this is.dave08
01/04/2023, 4:45 PMlist1..groupBy { it.id }
.mapValues { it.value.maxBy { entry -> entry.version } }
because I only need the highest version for that id... the other option (maybe better) could be to make the result Map<String, Pair<Foo, List<Baz>>
and let my resolvers decide what to output from each of those map entries and one step would be comparing those versions...
That was my goal. to have some processors process each entry they might care about and output an end result for it.dave08
01/04/2023, 4:46 PM