Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8881

Re: constructing a constant HashMap

From Tom Anderson <twic@urchin.earth.li>
Newsgroups comp.lang.java.programmer
Subject Re: constructing a constant HashMap
Date 2011-10-17 00:57 +0100
Organization Stack Usenet News Service
Message-ID <alpine.DEB.2.00.1110170052390.27716@urchin.earth.li> (permalink)
References <ktbk975fhdiok67oitle60ihoqhlrt1ml1@4ax.com> <49CdnacDvODl-wfTnZ2dnUVZ_hmdnZ2d@earthlink.com> <j7dq4g$8tv$1@speranza.aioe.org>

Show all headers | View raw


On Sun, 16 Oct 2011, B1ll Gat3s wrote:

> On 16/10/2011 1:07 AM, Patricia Shanahan wrote:
>> Roedy Green wrote:
>>> What is your preferred way of building a HashMap when all the values
>>> are known at compile time?
>> 
>> If the map is a field, I often use an instance or static initializer
>> immediately after the map's declaration:
>> 
>> Map<String,String> myMap = new HashMap<String,String>();
>> {
>> myMap.put("aaa", "bbb");
>> ...
>> }

For static final variables (suggested by "all the values are known at 
compile time"), i would suggest a slight variation:

static final Map<String,String> myMap;
static {
 	Map<String,String> tmpMap = new HashMap<String,String>();
 	tmpMap.put("aaa", "bbb");
 	myMap = Collections.unmodifiableMap(tmpMap);
}

> This works in ANY setting where an expression of type Map<String,String> is 
> appropriate:
>
> new HashMap<String,String> () {
>    {
>        put("aaa", "bbb");
>        ...
>    }}

This is called a 'double brace initializer', and it is a very useful but 
also highly surprising construct (i love dropping one in front of my pair 
when pair programming, and watching their brains trying to work out what's 
going on - it takes a while to realise it's not a special syntax, just a 
combination of two other bits of syntax). See:

http://c2.com/cgi/wiki?DoubleBraceInitialization

I use this form a lot in unit tests, when i need to set up a map quickly, 
but less so in 'real' code. It creates lots of anonymous subclasses, which 
i am nervous about doing.

tom

-- 
So the moon is approximately 24 toasters from Scunthorpe.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

constructing a constant HashMap Roedy Green <see_website@mindprod.com.invalid> - 2011-10-15 18:14 -0700
  Re: constructing a constant HashMap Arne Vajhøj <arne@vajhoej.dk> - 2011-10-15 21:36 -0400
    Re: constructing a constant HashMap Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-10-16 16:51 +0200
  Re: constructing a constant HashMap markspace <-@.> - 2011-10-15 18:59 -0700
    Re: constructing a constant HashMap Roedy Green <see_website@mindprod.com.invalid> - 2011-10-15 19:25 -0700
  Re: constructing a constant HashMap Roedy Green <see_website@mindprod.com.invalid> - 2011-10-15 19:19 -0700
  Re: constructing a constant HashMap Patricia Shanahan <pats@acm.org> - 2011-10-16 06:07 +0100
    Re: constructing a constant HashMap B1ll Gat3s <wm.g4t3s@m1cr0s0f7.c0m> - 2011-10-16 01:29 -0400
      Re: constructing a constant HashMap Tom Anderson <twic@urchin.earth.li> - 2011-10-17 00:57 +0100
        Re: constructing a constant HashMap Patricia Shanahan <pats@acm.org> - 2011-10-17 01:02 +0100
      Re: constructing a constant HashMap Arne Vajhøj <arne@vajhoej.dk> - 2011-10-16 22:20 -0400
        Re: constructing a constant HashMap B1ll Gat3s <wm.g4t3s@m1cr0s0f7.c0m> - 2011-10-17 21:35 -0400
          Re: constructing a constant HashMap Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-18 09:28 -0700
            Re: constructing a constant HashMap David Lamb <dalamb@cs.queensu.ca> - 2011-10-20 12:55 -0400
    Re: constructing a constant HashMap Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-16 11:11 -0300
  Re: constructing a constant HashMap Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-16 10:39 -0700

csiph-web