0

For a Map by itself how come I can do

Map<Object, Object> map1 = new HashMap<Object, Object>();

But for a Map in a Map, I cannot do

Map<Object, Map<Object, Object>> map2 = new HashMap<Object, HashMap<Object, Object>>();

Instead, I must do

Map<Object, HashMap<Object, Object>> map3 = new HashMap<Object, HashMap<Object, Object>>();

This situation isn't exclusive to Maps, it also pertains to Set and I'm assuming all other interfaces. In other words, I cannot use interfaces as Generics. Why not?

onepiece
  • 3,279
  • 8
  • 44
  • 63
  • Why not just do: `Map> map2 = new HashMap>();`?? This would work **and** would be much cleaner as it codes to the interface. – Hovercraft Full Of Eels Sep 26 '14 at 00:39
  • @HovercraftFullOfEels hmm it's weird that's possible...I thought I have to implement interfaces (use `HashMap` instead of `Map` in the last part) when I instantiate. – onepiece Sep 26 '14 at 08:18
  • There's nothing weird about it as it's in the *declaration* portion of the variable, even though it's on the right side, since you've not yet created Maps to go into that slot. – Hovercraft Full Of Eels Sep 26 '14 at 10:37

0 Answers0