Method in the type Map is not applicable

后端 未结 1 1239
无人及你
无人及你 2020-12-01 12:29

I have the following JAVA method implemented due a interface:

public String importDocument(ImportSource source, Map

        
相关标签:
1条回答
  • 2020-12-01 12:45
    ? extends Object
    

    You are using generic wildcard. You cannot perform add operation as class type is not determinate. You cannot add/put anything(except null).

    For more details on using wildcard you can refer oracle docs.

    Collection<?> c = new ArrayList<String>();
    c.add(new Object()); // Compile time error
    

    Since we don't know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.

    0 讨论(0)
提交回复
热议问题