Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in Generics code

Tags:

java

generics

Why is the below code giving a compile time error.

    Map<String,? extends Object> inputMap = 
                 new HashMap<String, ? extends Object>();

The compile time error.

Cannot instantiate the type HashMap<String,? extends Object>

I want a map with String as key and which takes any object as value

like image 814
user2434 Avatar asked Aug 14 '26 09:08

user2434


1 Answers

? does not mean "takes any object". It means "take a specific type of object, that happens to be unknown", which doesn't make sense when actually creating a container.

Try this instead:

 Map<String,Object> inputMap = new HashMap<String,Object>();
like image 183
Oliver Charlesworth Avatar answered Aug 16 '26 22:08

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!