0

Please help me! There i have two pieces of code:

1:

public static <K, V> void addElement(Map<K, Collection<V>> map) { }  
public static void main(String[] args) {
    Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
    addElement(map);
} 

2:

public static <V> void addElement(Collection<V> collection) { }  
public static void main(String[] args) {
    List<Integer> list = new ArrayList<Integer>();
    addElement(list);
} 

The code of pieces 1 cannot be compiled!when i try to compiled, jvm tell me:'Map<String, List> cannot be converted to Map<K, Collection>'

Some of my friends tell me that the list unable upcast to collectoin, but the code of pieces 2 can be compiled!

so What is the reason that the first code cannot be compiled?

  • 1
    Replace `List` in the linked question with `HashMap`, replace `Dog` in the linked question with `List`, and replace `Animal` in the linked question with `Collection`, and you'd get your question. – Sweeper Sep 28 '20 at 02:42
  • @Sweeper Thank you very much for your reply. My English is not very good, so I don't know which keywords to use to search for this problem. I feel shy. – 时间只会一直走 Sep 28 '20 at 02:45

0 Answers0