Hi developers,
I would need one advice. I have collection of data ( 1000 instances of Car). I have one thread to change data and multiple threads to access data.. I need to access it by id, so MutableMap is selection. But I need also filter data out by range of lon, lat to calculate the closest few.
What would be the fastest and the best approach?
Currently I have (code bellow), but this searches through all elements.
data class Car(
val id : Long,
val registration : String,
val lon: Float,
val lat: Float
)
s
spand
10/15/2020, 12:43 PM
You need to use a thread safe collection if you want to modify and read at the same time
m
Michael de Kaste
10/15/2020, 12:55 PM
two different collections should probably be used.
One is easy, a hashmap that stores from ID to Car.
The other one is tougher.
m
Marshall
10/15/2020, 2:13 PM
One way to not search through all cars is to keep a structure that organizes cars by grid, then select grids to search based on given min/max lat/lon
Marshall
10/15/2020, 2:14 PM
you would have to assign cars to grids every time their lat/lon changes
j
James Richardson
10/15/2020, 7:14 PM
Why do you need the fastest and best approach? Id start with any old thing that works, and then see if its fast enough. If it is, then you are done!
m
Michael de Kaste
10/15/2020, 7:17 PM
Big oof.
Untill you get to later stages of product development and you're spending months on retention, debugging and analyzing focussed on why your software has slowly been dragged into the drain. Sluggishly popping up a loading circle as the backend has O(n²) request after O(n²) request