Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Substring Search in a Map

Thread view: 
kirthiga.narayanan@gmail.com - 28 Dec 2005 10:58 GMT
Hi,

I have a hash map with both the keys and the values as
strings...say emp id as the key and employee name as value.
eg: EMP1--->John Smith
    EMP2---->AolSmith

Now I enter a search key say "Smith" or "th" or "S"
and I need to find out all the values in the map which contains the
search key. In the above examples, the values "John Smith" and
"AolSmith" have to be returned...

If the search Key is "h", then John Smith alone shud be returned...
Remember its a SUBSTRINGSEARCH.........
I can use Linear Search but when
the size of the map increases the performance decreases rapidly...
is there any other way to do it.......Please let me know ASAP.......
Even if means storing in a different data structure I dont mind....

Thanks
Kirthiga
Roedy Green - 28 Dec 2005 11:32 GMT
>I have a hash map with both the keys and the values as
>strings...say emp id as the key and employee name as value.
[quoted text clipped - 12 lines]
> is there any other way to do it.......Please let me know ASAP.......
>Even if means storing in a different data structure I dont mind....

Your map goes looks for EMP1.  You need a second TreeMap that goes the
other way.  You need to massage your keys ahead of time to look like
"Smith John" so that you can look up with approximate key.

There is no substring search by key.. For that you need to scan the
entire set of keys linearly.  Best to use an SQL engine that will
optimise such searches.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Googmeister - 28 Dec 2005 12:05 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>  is there any other way to do it.......Please let me know ASAP.......
> Even if means storing in a different data structure I dont mind....

You can form all of the suffixes and sort them (or put them
in a BST if you need to support insert/delete). Now,
you can use binary search to find any substring.

Here's the array for "johnsmith".

h
hnsmith
ith
johnsmith
mith
nsmith
ohnsmith
smith
th

When forming the suffixes, be sure to do it implicitly,
say using substring(), so that it takes only linear
space (and not quadratic!).

See also suffix arrays and suffix trees for
more sophisticated (and complicated) versions.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.