I have a situation where I'd like to be able to enter two string keys and get back an integer value.
I've tried doing this with a nested HashMap, but it really doesn't work the way I was doing it.
I was trying to create the nested entry in hash maps like this:
this.mapUserNames.put("Fred",mapUserType.put("User",1)); // 1 is perm.
Is there a way in Java to do this?
The code above compiles OK, but trying to retrieve the value using the opposite approach just gives me null.
So, I'd like to be able to enter: something like someMethod("Fred", "User") and get back Fred's permission. I don't want to have to do a DB lookup each time so I want to be able to store all the user name - user type values along with the related permission in some structure that I can access quickly like a map.
Look forward to any replies.
Joined: Nov 08, 2007
which part is returning null? you should be able to set it up like that (though /i/ wouldn't want to)
i'm not sure i understand the significance of all the parts,
is Fred of type User? and Users have a permission of 1?
no need to put a map in a map, map1 Fred -> User map2 User -> 1
if this doesn't solve the problem, please explain in more detail, more code is good,
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Now to get back Fred's permission you just stack them up in some sort of Collection with new's and add them with User.name as the key. Any key'ed collection will do this. If you just need to get by key, use the first one you can understand. If you need to get an ordered list, you will have to see which keyed collections will actually do this.
Joined: Jul 12, 2002
The basic thing I'm trying to do is be able to do a lookup for an integer, given two strings. Kind of like you'd be able to do if Java allowed arrays with different data types.
I can already write a loop to iterate through a set of beans and find the right one for the passed in two strings, but my intention with the "nested hashmap" idea was to be able to use both strings as keys to do a quick lookup without looping.
Joined: Mar 05, 2008
I think Nicholas' post may just be confusing the issue, since you don't need sorting, and since the User should not be the same object as the Comparator. But if you look at Rob's post, his advice seems to apply directly to what you're trying to do. Put the values into the maps the way he showed, and then you can retrieve them:
The code looks a little strange since you're apparently using autoboxing but not generics. I tried to keep the same style.
Another way to do this is with a single Map that uses composite keys. Create a new class that can contain two strings, such as "Fred" and "User1". Override equals() and hashCode() so that they depend on these two strings. Then use objects of this class as keys in your Map:
[ March 08, 2008: Message edited by: Mike Simmons ]
Joined: Sep 17, 2006
Mike It's your second approach I was trying to work to, I was just doing one it step at a time. If someone has a way to find that is neither iterating nor reliant on sorting prior to the search then it would probably be pretty chaotic. ( pun intended ) If you want to get fancy you could implement a trie ( not a tree ), but of importance is whether the two strings vary independently.
Joined: Mar 05, 2008
Originally posted by Nicholas Jordan: If someone has a way to find that is neither iterating nor reliant on sorting prior to the search then it would probably be pretty chaotic. ( pun intended )
Do you consider a HashMap chaotic? If you iterate through it, the order may appear random, but otherwise it's pretty straightforward and reliable.