• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use nested HashMap?

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic