Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Java HashMap or similar question

Java HashMap or similar question
Thread Tools
Fresh-Faced Recruit
Join Date: Apr 2001
Location: brooklyn, ny
Status: Offline
Reply With Quote
Feb 20, 2002, 03:25 PM
 
So I have this HashMap that contains the key "memberID" and value "memberName". this of course works great when I want to do a lookup on the key (memberID). But I will have the situation where I need to do a lookup on the value (memberName) and get the key back. I don't see a method to implement this and maintaining two HashMaps that are the same except for having reversed key, value pairs just doesn't seem right. Any suggestions?

both memberID and memberName are String objects.

Thanks.
     
Dedicated MacNNer
Join Date: Oct 2000
Location: Washington, DC
Status: Offline
Reply With Quote
Feb 20, 2002, 04:46 PM
 
You can use the .hasValue() method. This only returns boolean on whether the value exists in the HashMap. It doesn't return the key.

You could do two things (off the top of my head):

1. do a .hasValue() to see if such a string exists in the HashMap. If it does, you can then do an iteration through all the keys to locate the key/value pair.

2. Use something other than a HashMap. There are various Collections implementations which are all built to serve different specific purposes. AFAIK.. HashMap is built to be an extremely speedy way to store and look up values by keys alone, and not other purposes.

Someone with more Java knowledge may be able to offer more insight on best practices.

-tim
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Feb 20, 2002, 06:22 PM
 
hmm...
my gut reaction is that there may be a design flaw here.
Could you explain the situation with more detail?
The only way to do what you are asking is to get an iterator over the entry set of the collection and cycle through each one looking for the right value, which is an expensive operation.
There may be another way to solve your issue. Give us more detail
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Fresh-Faced Recruit
Join Date: Apr 2001
Location: brooklyn, ny
Status: Offline
Reply With Quote
Feb 21, 2002, 10:57 AM
 
Thanks for the replies so far.
I've looked through all the collections and there's nothing obvious. HashMap is the closest I see right now. Good point that this is really a design issue. I'm still designing the application.

I have this bulletin board system where people have a login (memberName), password, and memberID. The system (thanks to Java and Unicode) allows Japanese characters in the login (memberName). I also have a mailbox application where members can send messages to each other. Since some people on the system may not be able to input Japanese characters I allow them the ability to send messages to people using the other person's memberID. Basically I am anticipating (maybe incorrectly) the situation where I have the memberID and need to look up the name or possibly conversely the situation where i have the memberName and need to look up the memberID.

I can see ways to do this. One is (as suggested) iterating through and doing an equals() on all the values. Or maintaining two reversed HasMaps and looking up in the appropriate one based on what the method is passed (memberName or memberID).

Hmm. I may try the two HashMaps first. Maybe something will shake out.

Thanks.
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Feb 21, 2002, 01:28 PM
 
Are you using a database as the persistent storage?
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Fresh-Faced Recruit
Join Date: Apr 2001
Location: brooklyn, ny
Status: Offline
Reply With Quote
Feb 21, 2002, 02:45 PM
 
I am using MySql to store the user information so I could just do lookups on the user table but I am trying to only do an intial database connection for when the user logs in and when the web application (I'm not sure if I mentioned but this is a JSP/servlet/bean app running on tomcat on mac OS X). After that everything is stored in beans that I serialize out to the disk. So ideally i would like to have a bean that holds the memberName and memberID so I can easily do the following

public String getMemberID(String memberName) {
//lookup based on memberName
return memberID;
}

and

public String getMemberName(String memberID) {
//lookup based on memberID
return memberName;
}

So two HashMaps would work as would a lookup in the MySql table. But I haven't figured out another option yet.

Thanks.
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Feb 21, 2002, 08:00 PM
 
have you though of using EJBs?
Jboss works fine on OS X.
If you use CMP entity beans, your database roundtrips will be minimized.
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Fresh-Faced Recruit
Join Date: Apr 2001
Location: brooklyn, ny
Status: Offline
Reply With Quote
Feb 21, 2002, 10:09 PM
 
I'll probably stay away from EJBs for the moment because my current hosting provider only offers Tomcat.
     
Dedicated MacNNer
Join Date: Oct 2000
Location: Washington, DC
Status: Offline
Reply With Quote
Feb 22, 2002, 11:41 AM
 
I ran into this same design issue when I was developing another type of web application, and what I did was look at the application as a whole.

I figured that looking up usernames by ID would be like 90% of the situation. There would be the occasional situation when I would need to know the userid of a certain username.

So what I did was just stick with just a HashMap with userIDs as keys, then wrote a small fast routine that looked up the database for user IDs using the username. doing a SELECT on a single column is pretty fast anyway, its not a very complex SQL query. I figured that'd just be a whole lot easier to do. Then i just call the routine when I need to know the ID, and use that.

I think thats a lot easier than making sure the hashmaps are coordinated and all that. I dunno. I moved on a different project and havent really went and tested out this approach extensively. It may be a bad idea, who knows.

-tim
     
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status: Offline
Reply With Quote
Feb 23, 2002, 04:28 PM
 
I was wondering if you couldn't use an array(or vector) instead and simply map the userID's to index numbers or simply fill two arrays(or vectors) where the userID has the same index as the userName or simply a two dimensional array?
weird wabbit
     
Forum Regular
Join Date: Jun 2001
Status: Offline
Reply With Quote
Feb 24, 2002, 12:47 PM
 
Originally posted by theolein:
<STRONG>I was wondering if you couldn't use an array(or vector) instead and simply map the userID's to index numbers or simply fill two arrays(or vectors) where the userID has the same index as the userName or simply a two dimensional array?</STRONG>
Hashing uses arrays, but are faster because you know exactly where it is in the array.
"It's not like Windows users don't have any power. I think a lot of people are happy with Windows, and that's an incredibly depressing thought." -Steve Jobs
     
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status: Offline
Reply With Quote
Feb 25, 2002, 11:56 AM
 
Originally posted by discstickers:
<STRONG>

Hashing uses arrays, but are faster because you know exactly where it is in the array.</STRONG>
I know, but it comes down in either case to using enumerations or using the indexOf(Object o); method which does the work for you in a vector. from there you could just take the index and get() the userID from the other list and vice versa. I don't know whether an enumeration would be faster. If it is then use it I suppose.
weird wabbit
     
Fresh-Faced Recruit
Join Date: Apr 2001
Location: brooklyn, ny
Status: Offline
Reply With Quote
Feb 25, 2002, 03:51 PM
 
I think kristoff (it's a design flaw) and timster (look at the application as a whole are right. I was trying to combine functionality because it looked similar but the two situations will be different and unrelated. I will need to look up the memberName value by the memberID key often. But it will only be the occasional quirky situation when I need to do the reverse. I'll definitely stick with a HashMap for the first situation and then try to figure out why i need to do the second at all.
Thanks all.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2