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
Joined: Aug 17, 2001
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.
Sun Certified Programmer for the Java 2 Platform
Joined: Jul 18, 2002
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:
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.