This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to use nested HashMap? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to use nested HashMap?" Watch "How to use nested HashMap?" New topic
Author

How to use nested HashMap?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 949
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.

Thanks.

Mike
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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,

Fred/User/1

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);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216


mapUserType.put("User1", 1) returns the old value for mapUserType.get("User1"), before the put. If there is no value yet that will be null of course.

What you more lilely want to do is something like this:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282


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.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 949
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.

Mike
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
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 ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to use nested HashMap?
 
Similar Threads
struts - how to display table
java temperature converter
Retrieve Form Values - null form values returned
String processing
Authorization in JSF based on permission of a component