Hi developers, I would need one advice. I have c...
# announcements
j
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.
Copy code
fun nearCar(minLat: Float, maxLat: Float, minLon: Float, maxLon: Float): List<Car> {
    return mutableMapCars.values.parallelStream().filter {
        it.lat > minLat &&
                it.lat < maxLat &&
                it.lon > minLon &&
                it.lon < maxLon
    }.toList()
}
Copy code
data class Car(
    val id : Long,
    val registration : String,
    val lon: Float,
    val lat: Float
)
s
You need to use a thread safe collection if you want to modify and read at the same time
m
two different collections should probably be used. One is easy, a hashmap that stores from ID to Car. The other one is tougher.
m
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
you would have to assign cars to grids every time their lat/lon changes
j
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
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