Coolmore
Coolmore

Reputation: 11

Java Search by multiple keywords

I have a list of records containing country, city, district and building name information (more than 50,000 records) where building name is unique for every record.

I want to search building, district & city. But I want to get a list of cities if I pass the country to a method, e.g. get(String country). Or, get a list of districts if I pass country and city to the method, e.g. get(String country, String city).

Is there any existing collection/library/data structure to do something like this? I am thinking of a tree-like structure / Map. I tried MultiKeyMap, but it does not return a list of values and it is not thread-safe. Also, I don't want to use database for doing this.

Thanks in advance for your help.

Upvotes: 1

Views: 1302

Answers (5)

AardvarkSoup
AardvarkSoup

Reputation: 1081

Why not simply use three hashtables (e.g. of the type HashMap<String, List<Record>>): one keyed by buildings, one keyed by city and one keyed by district. Sure, you'll be using about three times as much memory; but 50,000 records really isn't that much. Furthermore, lookups will be really fast and simple. I'd recommend trying this and seeing how it performs.

Upvotes: 0

Samuel A Marchant
Samuel A Marchant

Reputation: 1

An off-beat type of way maybe using .properties files for each country to refer to a subset of localities in their each own .properties that again contains a a .properties to refer to cities that refer to .properties file containing buildings. Another may be a class hierarchy system with a base instantiated "new" class e.g. GeographicLocation with a constructor that is fed an index to load an abstract class that indicates a Region or brings back a list of regions if not indicated by calling one of the two methods overloaded and that in turn automatically loads the next abstract class layer of city over the top of that.

Inside GeographicLocation class ....
CountryMap cntry = (CountryMap)this();
RegionMap rgion = (RegionMap)cntry;
CityMap cty = (CityMap)rgion;
....e.t.c.

Upvotes: 0

jaychapani
jaychapani

Reputation: 1509

You can use HashMap like

HashMap<country,HashMap<City,HashMap<district,HashMap<building,value>>>>

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347332

You could take a look at Apache's Commons CollectionUtils. It has a "select" method that do what you want.

Upvotes: 0

npinti
npinti

Reputation: 52205

SolR might do the job you are after:

Solr is the popular, blazing fast open source enterprise search platform from the Apache Lucene project. Its major features include powerful full-text search, hit highlighting, faceted search, dynamic clustering, database integration, rich document (e.g., Word, PDF) handling, and geospatial search. Solr is highly scalable, providing distributed search and index replication, and it powers the search and navigation features of many of the world's largest internet sites...

It should allow you to create queries which will in turn allow you to search through your records.

You can also interact with SolR through Solrj:

Solrj is a java client to access solr. It offers a java interface to add, update, and query the solr index.

Upvotes: 1

Related Questions