开发者

Search/Queries in Collections (Java)

开发者 https://www.devze.com 2022-12-08 08:55 出处:网络
I\'ve created many Geodata objects (name,postalCode,lat,lon). Now 开发者_JAVA技巧I want to put them into a collection to search for different entries later.

I've created many Geodata objects (name,postalCode,lat,lon). Now 开发者_JAVA技巧I want to put them into a collection to search for different entries later.

Everything should happen objectOriented/in-memory, so there shouln't be a need for a relational database.

Such a Query looks like:

  • Find lat and lon by name or plz
  • Find objects between LAT1,LAT2 and LON1,LON2

What collection is the best one for such a "simple" datastructure?

What complexity is needed for such a query? Can multithreading be a benefit? If it is, which collection is used at best for thread safety?

Is there a chance to write such queries in a key=>value database?


You could use an in-memory database.

This is good as relational databases are good for relational queries like these.... :-)


For home-made pure Java, you could use:

  1. Map, with the name as key
  2. Map, with the plz as key
  3. List<List<"object">> with LAT for the first list, LON for the second list.
    Both are sorted, so for each you can search for a value using binary-search, and return an interval efficiently with subList.

This amounts to a duplication for the keys, but not for all objects, as you can reuse the same instance objects in all of these cases.

Multi-threading is acceptable (if your need it for other reasons), but I doubt you need to introduce it to improve the performance of a single search. The data structures mentioned should deliver the correct answers in less than a millisecond !

Thread-safety is not a problem for these data structures, as your use case seem to be read-only. If you need to modify the "objects" in some case, then you can only protect the "objects" themselves, not the data structures used for searching.

0

精彩评论

暂无评论...
验证码 换一张
取 消