[英]Method in the type Map<String,capture#1-of ? extends Object> is not applicable
I have the following JAVA
method implemented due a interface: 由于接口,我实现了以下JAVA
方法:
public String importDocument(ImportSource source, Map<String, ? extends Object> paramMap);
When i try to do following, i get an compile warning. 当我尝试做以下时,我收到编译警告。 Snippet: 片段:
paramMap.put("Key", "Value");
Error: 错误:
The method put(String, capture#1-of ? extends Object) in the type Map is not applicable for the arguments (String, String) 方法put(String,capture#1-of?extends Object)在Map类型中不适用于参数(String,String)
Why? 为什么?
? 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). 你不能添加/放置任何东西(除了null)。
For more details on using wildcard you can refer oracle docs . 有关使用通配符的更多详细信息,请参阅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. 由于我们不知道c的元素类型代表什么,我们无法向其添加对象。 The add()
method takes arguments of type E
, the element type of the collection. add()
方法接受type E
参数,即集合的元素类型。 When the actual type parameter is ?
当实际的类型参数是?
, it stands for some unknown type
. ,它代表一些unknown type
。 Any parameter we pass to add would have to be a subtype of this unknown type. 我们传递给add的任何参数都必须是这种未知类型的子类型。 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
. 因为我们不知道它是什么类型,所以我们无法传递任何内容。 The sole exception is null, which is a member of every type
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.