2

Today, I faced an odd situation of generic array creation of Java 7. Take a look at following two statement.

 Map<String, String>[] hashArr= new HashMap[2]; // Compiles 

 Map<String, String>[] hashArr= new HashMap<>[2];// Does not compile

Here first statement compiles without diamond operator, if I put diamond operator or generic type at right side than it does not compiles. I faced same situation for all generic type, List<T>, Set<T>

Can anyone tell me, what is the reason for not compiles second statement?

Masudul
  • 21,045
  • 5
  • 38
  • 51
  • 1
    @Downvoter, please comment, what is the wrong of this question? – Masudul Dec 14 '13 at 06:37
  • Because you cannot create a generic array of type `HashMap`. – Elliott Frisch Dec 14 '13 at 06:41
  • @ElliottFrisch, I can create array with first statement. – Masudul Dec 14 '13 at 06:42
  • 1
    possible duplicate of [Array of Generic List](http://stackoverflow.com/questions/7810074/array-of-generic-list) – Paul Bellora Dec 14 '13 at 06:42
  • 1
    @PaulBellora, This is not duplicate of given link question. – Masudul Dec 14 '13 at 06:46
  • 1
    @ElliottFrisch, First statement compiles as well as works. You can run this code, `HashMap hm1 = new HashMap<>(); hm1.put("AB", "ABC"); HashMap hm2 = new HashMap<>(); hm2.put("CD", "ABC"); hashArr[0]=hm1; hashArr[1]=hm2; for (Map map : hashArr) { System.out.println(map); }` – Masudul Dec 14 '13 at 06:56
  • What are you asking then? You know the answer. The diamond operator fills in the left hand generics, so do the same as per my answer (and you get the correct error)... but you're creating one typed HashMap at a time (not an array). – Elliott Frisch Dec 14 '13 at 07:00
  • My question is to know the reason, why it is not compiles with diamond operator or generic type at right side of generic array. – Masudul Dec 14 '13 at 07:02
  • @Masud Assume it worked like my answer does... and fill in the diamond. Notice you actually get a more meaningful error. – Elliott Frisch Dec 14 '13 at 07:16
  • @Masud That isn't a helpful comment, without explaining why it's different. – Paul Bellora Dec 14 '13 at 07:19
  • Other duplicates: [Java Error: New Generic TreeNode Array](http://stackoverflow.com/questions/11166710/java-error-new-generic-treenode-array), [Cannot create an array of LinkedLists in Java…?](http://stackoverflow.com/questions/217065/cannot-create-an-array-of-linkedlists-in-java), [How does one instantiate an array of maps in Java?](http://stackoverflow.com/questions/1493162/how-does-one-instantiate-an-array-of-maps-in-java?rq=1), [array of parameterized types](http://stackoverflow.com/questions/9542076/array-of-parameterized-types) – Paul Bellora Dec 14 '13 at 07:26
  • possible duplicate of [What's the issue with creating a generic array?](http://stackoverflow.com/questions/18581002/whats-the-issue-with-creating-a-generic-array) – Rohit Jain Dec 15 '13 at 11:04

1 Answers1

2

You cannot create a generic array of type HashMap in java due to type erasure (the generic(s) are erased by the compilation step). This code

Map<String, String>[] hashArr= new HashMap<String,String>[2]; // gives a better error.

Your first statement is an array of untyped HashMap, I know it compiles. Does it work?

To my delight, this does work

Map<String, String>[] hashArr = new HashMap[1];
hashArr[0] = new HashMap<>();                    // Your diamond sir.
hashArr[0].put("Hello", "World");
System.out.println(hashArr[0].get("Hello"));
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226