0

Since using generics is a good practice in initialing HashMap and the following initialization works fine:

Map<String,String> x = new HashMap<String,String>();

Why do I have a type mismatch error in initialing the nested HashMaps:

Map<String,Map<String,String>> y = new HashMap<String,HashMap<String,String>>();
Noviff
  • 318
  • 3
  • 6

1 Answers1

1

There is an issue with declaration please replace above declaration with below line.

Map<String,HashMap<String,String>> y = new HashMap<String,HashMap<String,String>>();

If you look at exception you will get an better idea.

Type mismatch: cannot convert from HashMap<String,HashMap<String,String>> to Map<String,Map<String,String>>

It matches exact type. When your given type it might be exact same type. Instead of Map<String,String> you to use HashMap<String,String>

If you want to try another thing please try with below line as well.

Map<String,Map<String,String>> y = new HashMap<String,Map<String,String>>();
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59