I am trying to do something like this
final Map<String, ? extends Object> params = new HashMap<String, ? extends Object>();
but java compiler complaining about that “cannot instantiate the type HashMap();
whats wong with it..?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
? extends Objectis a wildcard. It stands for “some unknown type, and the only thing we know about it is it’s a subtype ofObject“. It’s fine in the declaration but you can’t instantiate it because it’s not an actual type. TryBecause you do not know what type the
?is you can’t assign anything to it. SinceObjectis a supertype of everything,paramscan be assigned be assigned a reference to bothHashMap<String, Integer>as well asHashMap<String, String>, among many other things. AStringis not anIntegernor is anIntegeraString. The compiler has no way of knowing whichparamsmay be, so it is not a valid operation to put anything inparams.If you want to be able to put
<String, String>inparamsthen declare it as such. For example,For a good intro on the subject, take a look at the Java Language tutorial on generics, esp. this page and the one after it.