views:

176

answers:

1

I've created many Geodata objects (name,postalCode,lat,lon). Now 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?

+3  A: 

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.

KLE
I want to solve this problem with pure java! The sorted structures seem nice. Isn't it better to use a tree with flat objects sorted with a comparator by lat/lon? There are also over 100.000 entries, so I'm not sure if a query will only need less than a millisecond.
Martin K.
@Martin - Some implementation of `Map`, such as `HashMap`, are very efficient at looking things up, it can easily find an object in less than a millisecond, even if your map contains more than 100.000 entries.
Jesper
Isn't there a better solution with composite keys and a treeset using the subSet method?
Martin K.
@Martin If your tree is sorted in one direction, you can only search on one direction, not on LAT and LON intervals. Example: if sorted on LAT (then LON for equal LAT values), what happens when you search on LAT 10-50, LON 17 exactly? The search finds a large result of compatible LATs, among which you have to check *all* entries to find only a small number of entries compatible! :-(
KLE
@Martin My previous comment assumed you wanted to replace the two Lists with only one Tree, sorted on a composite criteria. If you were thinking of replacing each List with a SortedSet, then yes, it will work also. Finding the correct value will not be faster than a binary search on a sorted List though. In addition, finding an interval (I think you need that) will be much more difficult...
KLE
Yeah I'm talking about finding an interval. Is it really so difficult in a tree structure?
Martin K.
@Martin First, what is the advantage you are looking for with a tree structure? I don't see any yet... Second, once you found the item you want in a tree, can you start from that point to find the end of the interval without starting from scratch? And once you found both ends of the interval, is there an efficient method provided to obtain all values that are in between? I don't think so, while these methods exists with List (subList, binary-search)...
KLE