Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6237556

Addition of Map initialising syntax

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • specification
    • x86
    • windows_xp

      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

            abuckley Alex Buckley
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: