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


Groups > comp.lang.java.programmer > #8844 > unrolled thread

constructing a constant HashMap

Started byRoedy Green <see_website@mindprod.com.invalid>
First post2011-10-15 18:14 -0700
Last post2011-10-16 10:39 -0700
Articles 2 on this page of 22 — 12 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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 Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 16:16 -0500
              Re: constructing a constant HashMap Lew <lewbloch@gmail.com> - 2011-11-06 14:37 -0800
                Re: constructing a constant HashMap Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 18:00 -0500
                  Re: constructing a constant HashMap Ian Pilcher <arequipeno@gmail.com> - 2011-11-06 18:34 -0600
                    Re: constructing a constant HashMap Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 19:47 -0500
              Re: constructing a constant HashMap B1ll Gat3s <wm.g4t3s@m1cr0s0f7.c0m> - 2011-11-07 12:23 -0500
      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

Page 2 of 2 — ← Prev page 1 [2]


#8863

FromArved Sandstrom <asandstrom3minus1@eastlink.ca>
Date2011-10-16 11:11 -0300
Message-ID<QCBmq.1810$Xd6.1390@newsfe07.iad>
In reply to#8855
On 11-10-16 02: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");
>   ...
> }
> 
> Patricia

Same here, actually. Glad you pointed that out. Doing it this way is
more clear than having some init() method some place.

AHS
-- 
I tend to watch a little TV... Court TV, once in a while. Some of the
cases I get interested in.
-- O. J. Simpson

[toc] | [prev] | [next] | [standalone]


#8868

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2011-10-16 10:39 -0700
Message-ID<YEEmq.1679$804.1480@newsfe05.iad>
In reply to#8844
On 10/15/11 6:14 PM, Roedy Green wrote:
> What is your preferred way of building a HashMap when all the values
> are known at compile time?


public class StaticHash {
    private static final Map<String, String> map;

    static {
       Map<String, String> mutableMap = new HashMap<String,String>();
       mutableMap.put("First Key", "First Value");
       mutableMap.put("Second Key", "Second Value");
       mutableMap.put("Third Key", "Third Value");
       map = Collections.unmodifiableMap(mutableMap);
    }
}

Or

enum MyKeys {
    FIRST("First Value"),
    SECOND("Second Value"),
    THIRD("Third Value")
    ;
    private final String value;
    private MyKeys(String value) { this.value = value; }
    //... if you need a reverse look-up, static initializer (as above)
    // that builds the reverse look-up map
}

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.java.programmer


csiph-web