United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6237556 : Addition of Map initialising syntax

Details
Type:
Enhancement
Submit Date:
2005-03-08
Status:
Closed
Updated Date:
2010-04-04
Project Name:
JDK
Resolved Date:
2008-05-21
Component:
specification
OS:
windows_xp
Sub-Component:
language
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
6
Fixed Versions:

Related Reports
Duplicate:
Relates:
Relates:
Relates:

Sub Tasks

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

I think this is a fine idea. Won't happen soon, but maybe someday.

###@###.### 2005-04-26 17:47:37 GMT
                                     
2005-04-26
EVALUATION

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



Hardware and Software, Engineered to Work Together