2

I have a method that expects a generic map in parameter :

public void someMethod(Map<String,?> mapParameter){ ... } 

My implementation that calls this method, have multiple possible values to the "?" generic param. For example, sometimes the value in "?" will be Integer, others it will be String, Boolean.

Exists some way to declare a fully generic HashMap that can accept my values, and passed to the "someMethod" method ?

I tried to extract the values to classes "IntegerValueDTO", receiving the value in String value and converting to the especific type. All this "valueDTO" classes created, i put to extend from "defaultValue". With this, i implemented this map :

Map<String, ? extends DefaultValue> map = new HashMap<String, ? extends DefaultValue>(); 

But, the method someMethod is not from my application, it is from a framework, then i dont have the control to chance the implementation. And declaring my map that way, he will generate a map of "Map<String, some instance of DefaultValue>", and the framework crashes.

Any help will be welcome.

2
  • I don't quite understand the question. There is this method that you describe. You want to call it. What is the problem? You should be able to call it with a Map<String, ?>, or a Map<String, Foo>, or a Map<String, ? extends Foo>. Is the problem writing something that compiles, or something that runs? Commented Jul 24, 2012 at 12:40
  • Do not place "solved" in the title of a question. If you have an answer, then post an answer and accept that. I've rolled your edit back. Please submit an answer. Commented Aug 6, 2012 at 11:56

2 Answers 2

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

If that method returns some values that you expect to use in your application, you can use reflection to see what value it returns and cast to that particular object.

Sign up to request clarification or add additional context in comments.

2 Comments

@Tom: You are right. We have to declare a variable of that type and instantiate it depending on the type of argument being passed to that method. ex: new HashMap<String, Integer>();. @Raduq Santos: I don't quite understand the question though. You say you want to pass the Map as a argument, but you also you are extracting values to DTOs. What exactly are you trying to achieve?
i'm trying to create a map fully gereric, that can store String, or integer, or boolean, but not create objects. I think that's not possible in my structure.
0

You can't do that without a Wrapper class like your DefaultValue.

The reason is that the Wrapper classes for Integer, Character, Long etc. do not extend Object.

2 Comments

what I mean is that you can't write extends Object but you can decleare a map like this Map<String,Object> but not Map<String,? extends Object>
You can declare a Map<String,? extends Object>. You can't instantiate that or extend it, but you can declare a variable of that type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.