https://kotlinlang.org logo
#codereview
Title
# codereview
t

Tuang

12/23/2019, 9:46 PM
i need review for my code
Copy code
val carMst: List<(Mutable)Map<String!, Any!>!>!
val carUserIds: List<(Mutable)Map<String!, Any!>!>!
....
....
val result = ArrayList<Car>()
for (mst in carMst) {
    val userIds = ArrayList<Int>()
    for (uids in carUserIds) {
        if (mst["car_id"] == uids["car_id"]) {
            userIds.add(uids["user_id"] as Int)
        }
    }
    val car = Car(
        mst["car_id"] as Int,
        mst["model"] as String,
        userIds
    )
    result.add(car)
}
i dont know too much about collection and lambda but with collection this can be more simple and beautiful right? 😀 thanks
s

Shawn

12/23/2019, 10:04 PM
is each
Map<String, Any>
representative of some kind of
Car
-related record stored somewhere? You should consider migrating from weakly-typed maps to something more explicit and harder to coerce improperly
is
if (carMst["car_id"] == uids["car_id"])
supposed to be
if (mst["car_id"] == uids["car_id"])
?
carMst
is a list here and cannot be accessed using a string
I’m not sure if this snippet even compiles tbh
the implied data model here seems complex and hard to advise on over the internet not gonna lie
I’m not saying there’s guaranteed to be a better than
O(mn)
solution here, but there very well might be one
the usage of
as
here is also pretty sus
t

Tuang

12/23/2019, 11:33 PM
thank you
4 Views