0

I have three lines of code :

1) List<String> list = new ArrayList<String>();

Which does not produce any error
but when I write following line of code

2)Map<String, List<String>> map = new HashMap<String, ArrayList<String>>();

I get following Error
Type mismatch: cannot convert from HashMap<String,ArrayList<String>> to Map<String,List<String>>

3)Map<String,String> d= new HashMap<String,String>();

This line does not produce any error

I want to know why
(2) line shows me Error. Thanks in Advance.:)

mcacorner
  • 1,304
  • 3
  • 22
  • 45

3 Answers3

6

Here's an informal answer. With this code:

Map<String, List<String>> map = new HashMap<String, ArrayList<String>>();

you'd end up with a map whose value can be any List<String>, which means you could do this:

map.put("test", new LinkedList<String>());

and this would be incorrect since a LinkedList is not an ArrayList. The Java compiler is making an effort to provide type safety at compile time and that's why it generates an error.

jdigital
  • 11,926
  • 4
  • 34
  • 51
  • **Confusing** explanation if you ask me. He never said that he wanted a Map that would only accept ArrayLists as values. What would be the point of that? He more likely wants a Map that accepts any List as a value, but he is having trouble instantiating it. – Alvin Thompson Jun 06 '14 at 05:42
  • @AlvinThompson True, but he created a HashMap that will only accept an ArrayList as a value, hence the problem. I'd be happy to improve the wording on this if you have a suggestion. – jdigital Jun 06 '14 at 05:45
  • That's not the problem. The problem is simply that type doesn't match the type of the variable he's trying to assign it to. – Alvin Thompson Jun 06 '14 at 05:47
  • @AlvinThompson Take a look at the line labelled (1) in the original post. The left and right hand side types are not identical, yet this is (of course) legal Java. He wants to know why he can't do a similar thing on line (2). – jdigital Jun 06 '14 at 06:06
  • First, the types **are** identical on line 1 (``). Second, he's not trying to do a similar thing on line 2 (`>` vs `>`). Third the fact that the types don't match was **my** point. You answer states that the problem was that the Map could accept an ArrayList as a value, which you state is "wrong". Since the first thing he wrote is (obviously) the lvalue, his clear intent was to create a Map which accepts any type of List as values. Saying that accepting `LinkedList`s is "wrong" is confusing from that perspective. – Alvin Thompson Jun 06 '14 at 06:18
  • To state it another way, your answer has very likely led the asker to believe that he needs to make the lvalue type more specific (`>`) in order to solve his problem. Not only is this not true, it's unnecessarily limiting. – Alvin Thompson Jun 06 '14 at 06:25
2

The declared type of your Map variable (<String, List<String>>, on the left) must match the type you're trying to assign to it (on the right). Since the types don't match, it's a similar error as if you tried to write:

Map<String,String> d= new HashMap<Integer,String>(); // Types don't match!

Instead, make sure the created type matches, like this:

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

You can then add ArrayLists (or any type of List) as Map values as needed:

List<String> list = new ArrayList<String>();
map.put("foo", list);

Or skip the variable:

map.put("foo", new ArrayList<String>());
Alvin Thompson
  • 5,388
  • 3
  • 26
  • 39
0

You Can use this-

HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
Ben10
  • 37
  • 1
  • 1
  • 5