简体   繁体   English

类型Map <String中的方法,捕获#1-of? extends Object>不适用

[英]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.

相关问题 Java泛型编译错误-方法method(Class <capture#1-of ? extends Interface> ) <type> 不适用于参数 - Java generics compilation error - The method method(Class<capture#1-of ? extends Interface>) in the type <type> is not applicable for the arguments 验证器类型中的方法validate(capture#2-of?extended Object) <capture#2-of ? extends Object> 不适用于参数(字符串) - The method validate(capture#2-of ? extends Object) in the type Validator<capture#2-of ? extends Object> is not applicable for the arguments (String) 方法映射((<no type> cfg) -&gt; {}) 对于 List 类型是未定义的<capture#1-of ? extends Config> - The method map((<no type> cfg) -> {}) is undefined for the type List<capture#1-of ? extends Config> java generics - Comparable 类型中的方法 compareTo(capture#1-of?)<capture#1-of ?> 不适用于 arguments</capture#1-of> - java generics - The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments 泛型:无法从 <capture#1-of ? extends Object,D> 至 <S,D> - Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D> 方法 reduce(Task, BinaryOperator<Task> ) 在 Stream 类型中<Task>不适用于参数(HashMap <String,Map<String,Object> &gt; - The method reduce(Task, BinaryOperator<Task>) in the type Stream<Task> is not applicable for the arguments (HashMap<String,Map<String,Object>> Java泛型:Comparable类型的compareTo(capture#2-of?扩展E)方法不适用于自变量(Comparable) - Java Generics :The method compareTo(capture#2-of ? extends E) in the type Comparable is not applicable for the arguments (Comparable) 方法与 ? 扩展集合<String>不适用于 Set<String> - Method with ? extends Collections<String> is not applicable for Set<String> fromMessage(String,Class <T extends BaseObject> )中的MessageConverter类型不适用于参数(字符串,类 <T> ) - The method fromMessage(String, Class<T extends BaseObject>) in the type MessageConverter is not applicable for the arguments (String, Class<T>) ArrayList类型中的方法add(String) <String> 不适用于参数(对象) - The method add(String) in the type ArrayList<String> is not applicable for the arguments (Object)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM