2

How can I manually fill out the following HashMap?

public static final HashMap<String,int[]> AGE_GROUPS = {"18-24",{18,24},
                                                        "25-29",{25,29},
                                                        "30-39",{30,39},
                                                        "40-49",{40,49},
                                                        "50-59",{50,59},
                                                        "60-69",{60,69},
                                                        "70-79",{70,79},
                                                        "80+",{80,120}};
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • 1
    Definitely not like that. There are no map (or list) literals in Java, so you'll have to fill it the old fashioned way. – Kayaman Sep 04 '15 at 06:28
  • Final can't be set after declaration, so first you have to change then edit it. – SaviNuclear Sep 04 '15 at 06:28
  • have a look at http://stackoverflow.com/questions/9489384/initializing-a-guava-immutablemap using the guava ImmutableMap – Sean F Sep 04 '15 at 06:30
  • 1
    You can use double brace initialization: http://stackoverflow.com/questions/852822/java-arraylist-and-hashmap-on-the-fly Beware of the performance issues: http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization – mtyurt Sep 04 '15 at 06:43

2 Answers2

11

This is called as static initialization.

 private static final Map<Integer, String> myMap;
    static {
        Map<Integer, String> aMap = ....;
        aMap.put(1, "one");
        aMap.put(2, "two");
        myMap = Collections.unmodifiableMap(aMap);
    }

In your situation;

public static final Map<String, int[]> AGE_GROUPS;
    static{
        Map<String, int[]> otherMap = new HashMap<String, int[]>();
        otherMap.put( "10-20", new int[]{ 10, 11 } );
        otherMap.put( "20-30", new int[]{ 20, 21 } );

        AGE_GROUPS = Collections.unmodifiableMap( otherMap );

    }
Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
0

This is where I would use a helper method

public static Map<String, int[]> rangeMap(int... fromTo) {
    Map<String,int[]> map = new LinkedHashMap<>();
    for (int i = 0; i < fromTo.length; i += 2) {
        String key = fromTo[i] + (fromTo[i+1] > 100 ? "+" : "-"+fromTo[i+1]);
        map.put(key, new int[] { fromTo[i], fromTo[i+1]));
    return Collections.unmodifiableMap(map);
}

public static final Map<String,int[]> AGE_GROUPS = rangeMap(
    0, 17, 
   18, 24, 
   25, 29,
   30, 39,
   40, 49,
   50, 59,
   60, 69,
   70, 79,
   80, 120);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Nice, but won't generate the `"80+"` string for the final mapping. – Andreas Sep 04 '15 at 07:01
  • @Andreas fixed it. Though if you are over 120, you won't be 80+ – Peter Lawrey Sep 04 '15 at 07:11
  • 1
    I agree, but so far, only one person ever has been that old. I'd have gone with `999` or undefined, i.e. an `int[1]` value for the `"80+"` mapping, but that's OP's choice to make. – Andreas Sep 04 '15 at 07:20