Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6237556
Votes 0
Synopsis Addition of Map initialising syntax
Category java:specification
Reported Against
Release Fixed
State 11-Closed, duplicate of 4632701, request for enhancement
Priority: 4-Low
Related Bugs 4632701 , 4877954 , 4472509 , 5092855
Submit Date 08-MAR-2005
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" -> " customer ",
        "b" -> "boy",
        "c" -> "chair"
    };

The above syntax is just a suggestion - but any syntax of this  customer  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" -> " customer ",
        "b" -> "boy",
        "c" -> "chair"
    };

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

map.add("a", " customer ");
map.add("b","boy");
map.add("c","chair");
  xxxxx@xxxxx   2005-03-08 17:35:22 GMT
Work Around
N/A
Evaluation
I think this is a fine idea. Won't happen soon, but maybe someday.

  xxxxx@xxxxx   2005-04-26 17:47:37 GMT
4632701 asked for this and more some years earlier.
Posted Date : 2008-05-21 06:54:15.0
Comments
  
  Include a link with my name & email   

Submitted On 10-MAR-2005
AlexLamSL
Typo - "HashMap<String><String>" should read "HashMap<String, String>" instead ^^"


Submitted On 09-FEB-2007
hr_stoyanov
I do not see that much need for this proposal. Instead, try this:
--------------------------------
Map<String, String> map = new HashMap<String><String>()
{{
        put("a", " customer ");
        put("b","boy");
        put("c", "chair");
    }};
----------------------------------------------
which is perfectly valid (surprise!) language construct.


Submitted On 09-FEB-2007
hr_stoyanov
Ops ... what I meant was:
--------------------------------------------------------------------------------
Map<String, String> map = new HashMap<String,String>()
{{
        put("a", " customer ");
        put("b","boy");
        put("c", "chair");
    }};


Submitted On 21-FEB-2008
johnno62
the idea where x[i] compiles to x.get(i) and x[i] = a compiles to x.put( i, a ) would really improve the look of a lot of Java code. Could this be implmented as syntactic sugar in the compiler where x implements interface Map ?


Submitted On 22-FEB-2008
johnno62
If HashMap had a constructor with a variable but even number of object arguments (i.e. HashMap( Object... initialisers )) the following would be possible..

Map<String, String> map = new HashMap<String,String>(
        "a", " customer ",
        "b","boy",
        "c", "chair"
);



PLEASE NOTE: JDK6 is formerly known as Project Mustang