Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashtable - SQL Like operation

 
Krish
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to have sql's LIKE operation in hashtable(or anyother collection object) key lookup. Any idea how to do it.
Eg :
Hashtable hash = new Hashtable();
hash.put("KEY_1234","ONE");
hash.put("KEY_5236","ONE");
hash.put("KEY_6278","ONE");

hash.get("KEY_%23%"); --> should return KEY_1234 and KEY_5236.

Any idea how to do it without looping through the hash

Thanks
Anand
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do it using the standard Hashtable. You will have to use some kind of regular expression matching, which means looping through the collection unless you implement something really sophisticated.

You may want to check out Lucene which is a text search engine, that can do what you want.

Geoffrey
 
Krish
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Geoffrey.
I can't do the looping of keys because the number of keys in the hash is high(atleast 5000 keys).
I checked with Lucene too but they seem to be providing text level search and not w.r.to objects (unless I've overlooked).
Any other suggestions :roll:

Thanks
Anand
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How often do you iterate over those 5000 entries?
5000 doesn't look as a high value to me.
Did you test an iterating algorithm?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Krish",
Note that we require proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks,
Jeanne
Forum Bartender
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krish,
I agree with Stefan - 5000 isn't that many. Try it out and see how the performance is. If it turns out to be a bottleneck, you can split it into sublists by first character or something similar.

One suggestion: wrap the HashMap in your own data structure. This will make changing the implementation much easier if you need it at a later date.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic