Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you add a HashMap into a HashMap?  RSS feed

 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Trying to put an anonymous HashMap into an existing HashMap.. don't get any error but when i try to retrieve this inner HashMap I just get null.

myHashMap.put(keyName, new HashMap().put(name, Integer.valueOf("1")));

Can you not do an 'anonymous' hashmap? If it create a hashmap (name it) and insert it, it seems to work okay. But then I run into problems when retrieving this inner HashMap. It works okay for about 20 results then I get a null pointer exception.

Is there a better solution then putting HashMaps inside of HashMaps? I can't use a database.

Thanks
 
Ryan McGuire
Ranch Hand
Posts: 1142
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you always going to have the same set of keys for the inner HashMaps (much like the column names of a datbase table)? If so why not just make a new class with those keys as fields and then populate your outer HashMap with those?

Ryan
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt, what you are putting into the outer HashMap is not the other HashMap, but the return value of the call to the inner put method (which is null in this case).

It *is* possible to do what you want to do (inserting a filled HashMap without first assigning it to a local variable), using an instance initializer:



The outer brace makes the HashMap an anonymous inner class, the inner brace is an instance initializer (kind of an "anonymous constructor").

That's quite an advanced trick, though, and I'm not sure how many people would say that it's good style...
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matt Wilcko:
Is there a better solution then putting HashMaps inside of HashMaps? I can't use a database.


There almost certainly is a better solution, but we'll need more information from you in order to suggest a solution. Most importantly, what is your program supposed to do? You alluded to the need for database-like functionality without a database. That's fine, but it's a bit vague. What data are you trying to store and retrieve? Are you emulating a complex database with multiple tables of data that reference each other, or are you just storing a couple of unrelated lists of integers?
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


The outer brace makes the HashMap an anonymous inner class, the inner brace is an instance initializer (kind of an "anonymous constructor").

That's quite an advanced trick, though, and I'm not sure how many people would say that it's good style...


that's so nasty it's beautiful. thank you, i'll have to remember it - for the two, maybe three, times in my career when it'll be at least vaguely acceptable to use.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Beck:
i'll have to remember it - for the two, maybe three, times in my career when it'll be at least vaguely acceptable to use.
I've already earmarked it for the case, albeit infrequent, when I need to construct an object with multiple method calls on it in a constructor to be passed on to a chained constructor.

Since the call to super() or this() must be the first statement, it's always been a bear when you need to build a semi-complex object in the constructor to pass up the chain. This will work great! That's a hack worthy of a few beers at least.

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Harkness:

Since the call to super() or this() must be the first statement, it's always been a bear when you need to build a semi-complex object in the constructor to pass up the chain. This will work great! That's a hack worthy of a few beers at least.


I typically prefer to extract the initialization of the semi-complex object into a static creation method.

Where I use the above idiom, though, is for initializing semi-complex constants. It's better than using a static initializer, because it better communicates that the initialization belongs to the constant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!