Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method in the type Map<String,capture#1-of ? extends Object> is not applicable

I have the following JAVA method implemented due a interface:

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)

Why?

like image 382
Emaborsa Avatar asked Jan 10 '14 09:01

Emaborsa


People also ask

Why put method in map type is not applicable for string arguments?

Snippet: The method put (String, capture#1-of ? extends Object) in the type Map is not applicable for the arguments (String, String) Why? 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.

How to create a map in typescript?

The data structure holds key/value pairs and one particular key cannot appear more than one time. In TypeScript, you have a few easy ways to choose from when you want to create a map. A simple JavaScript indexed object with a mapped Typescript type ( easiest solution ). An indexed object with the Record utility type.

How do I convert a string to a map in Java?

Convert a String to a Map Using Streams To perform conversion from a String to a Map, let's define where to split on and how to extract keys and values: public Map<String, String> convertWithStream(String mapAsString) { Map<String, String> map = Arrays.stream (mapAsString.split (","))

What is a mapped type in Java?

A mapped type is a generic type which uses a union of PropertyKey s (frequently created via a keyof) to iterate through keys to create a type: In this example, OptionsFlags will take all the properties from the type Type and change their values to be a boolean.


1 Answers

? 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.

like image 193
Aniket Thakur Avatar answered Oct 18 '22 01:10

Aniket Thakur