JDK-6237556 : Addition of Map initialising syntax
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-03-08
  • Updated: 2010-04-04
  • Resolved: 2008-05-21
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
As with array initialising syntax:

//Case 1
int[] array = new int[]{0,1,2,3,4};
//Case 2
int[] array = {0,1,2,3,4};

It would be nice if I can do that with the Map class objects:

//Map or HashMap - it doesn't matter =P
Map<String, String> map = new HashMap<String><String>{
        "a" -> "apple",
        "b" -> "boy",
        "c" -> "chair"
    };

The above syntax is just a suggestion - but any syntax of this nature would make the use of Map's much easier. I reckon only "Case 1" is possible as "Case 2" would be ambiguous in which Map implementation to create the instance from - but if somehow we think, for example, that HashMap should be used by default that one can make available of "Case 2" as well.

JUSTIFICATION :
Since we have nice initialisers for Array's it would be handy to have these Map initialisers as well - and the fact that it is just a compile-time feature and would not do any harm to the language in terms of, for instance, backward-compatibility (do contradict me if this is not the case ^^")

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Map<String, String> map = new HashMap<String><String>{
        "a" -> "apple",
        "b" -> "boy",
        "c" -> "chair"
    };

ACTUAL -
Map<String, String> map = new HashMap<String><String>();

map.add("a", "apple");
map.add("b","boy");
map.add("c","chair");
###@###.### 2005-03-08 17:35:22 GMT

Comments
EVALUATION 4632701 asked for this and more some years earlier.
11-12-2006

EVALUATION I think this is a fine idea. Won't happen soon, but maybe someday. ###@###.### 2005-04-26 17:47:37 GMT
26-04-2005