类型Map中的方法< String,capture#1-of? extends Object>不适用 [英] Method in the type Map<String,capture#1-of ? extends Object> is not applicable
问题描述
我有以下 JAVA
方法实现由于接口:
I have the following JAVA
method implemented due a interface:
public String importDocument(ImportSource source, Map<String, ? extends Object> paramMap);
当我尝试执行以下操作时,
Snippet:
When i try to do following, i get an compile warning. Snippet:
paramMap.put("Key", "Value");
错误:
类型Map中的方法put(String,capture#1-of?extends Object)不适用于参数(String,String)
The method put(String, capture#1-of ? extends Object) in the type Map is not applicable for the arguments (String, String)
为什么?
推荐答案
? extends Object
您正在使用通用通配符。您无法执行添加操作,因为类类型不是确定的。你不能添加/放任何东西(除了null)。
You are using generic wildcard. You cannot perform add operation as class type is not determinate. You cannot add/put anything(except null).
有关使用通配符的更多详细信息,您可以参考oracle docs 。
For more details on using wildcard you can refer oracle docs.
Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error
因为我们不知道c的元素类型是什么,无法向其中添加对象。 add()
方法接收类型E
的参数,集合的元素类型。当实际类型参数?
时,它代表一些未知类型
。我们传递添加的任何参数都必须是这个未知类型的子类型。因为我们不知道是什么类型,所以我们不能传递任何东西。唯一的例外是null,它是每个类型
的成员。
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
.
这篇关于类型Map中的方法< String,capture#1-of? extends Object>不适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!