• 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

HashMap Construction with initial values

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I just came across this code below. I'm not clear how this works. Can you please explain me how this works?



I'm not understanding how this put() is working normally.

Thanks in advance
Binil
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code that is not easy to understand (and therefore maintain) is silly.

There are several things in play here.

1) an anonymous class
2) an instance block

Breaking it down, the anonymous class can be converted to an inner class:



Then the instance block can be replaced with a no-arg constructor

What it is missing is an unmodifiable Map, so better would be:


But I wouldn't do it in the first place

[Edit - added 'this' for clarity]
[Edit - forgot 'extends HashMap'. Rookie mistake ]
[ October 10, 2008: Message edited by: David O'Meara ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This expression creates an instance of an anonymous class which extends HashMap. The anonymous class includes an instance initializer block which contains the "put" calls. Are you familiar with either of these things yet? An anonymous inner class is a way of defining a subclass without giving it a name. An instance initializer block is basically a way to add code to all the constructors in a class. This code is really equivalent to



In any case, depending on who you ask, this is a cute hack, or a crime against humanity. It's not a very common idiom, and I hope it doesn't become moreso.

[ Edit: DRAT! Beaten! ]
[ October 10, 2008: Message edited by: Ernest Friedman-Hill ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally I wouldn't be so petty but since it's EFH: Woo hoo, a whole two minutes!

One thing is clear though, and that is neither of us would promote this practice. When in doubt, err on the side of legibility.
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how is that the anonymous class extends HashMap even without specifying the 'extends' keyword.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a new HashMap:

This is an anonymous Class that extends HashMap. The bit in the brackets is the 'extension stuff'

eg override default HashMap toString()
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it!
Thanks for the explanation
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
A lot of people are saying how they wouldn't do this. Would you mind listing a few alternatives?

All I can think of at the moment is a declarative approach, using a properties file or xml or somesuch, in which case, the benefit would be ease or change.

But this would a bit of hassle. I think the example given would be faster to code, and I personally don't find it hard to read at all.

Cheers,
Rene
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A more conventional approach would look like this:
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is declaring hm as final variable, that mean you dont need to use variable name to call a function,
it will access directly
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swapnil kataria wrote:it is declaring hm as final variable, that mean you dont need to use variable name to call a function,
it will access directly

Please explain that a bit more; it is by no means clear.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rene Wooller wrote:Hi All,
A lot of people are saying how they wouldn't do this. Would you mind listing a few alternatives?


Personally, if I wanted to expose this map as a public field, I would want to ensure that not only is the reference final, but the object itself is immutable. We don't want anyone calling put() or remove(0 on the map once it has been constructed. Traditionally this could be done like this:

Or this:

But nowadays I would prefer to use Guava:


Rene Wooller wrote:All I can think of at the moment is a declarative approach, using a properties file or xml or somesuch, in which case, the benefit would be ease or change.


Yeah, something like that could certainly be useful. If you want to load key-value pairs from some other source, I would put that loading code inside the initializeMap() method. You can do whatever you want there. If it needs more careful testing, it's easy to make the method package-private or some other access. The reason I often prefer a named method to a static initializer is that it's easier to test a named method.

Rene Wooller wrote:But this would a bit of hassle. I think the example given would be faster to code, and I personally don't find it hard to read at all.


Well, it's an unusual syntax that has just started to appear recently. A lot of people don't like it simply because they don't recognize it - or because they figure many other people will not recognize it. Which I think is a fair point. If I'm going to bring in something new that people might not be familiar with, I'd rather it be Guava, because that makes the code look much nicer and gives many other useful methods as well.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic