How Can I create a generic HashMap to insert collections and objects?

user2683519 picture user2683519 · Dec 18, 2013 · Viewed 56.4k times · Source

How Can I instantiate a HashMap to put collections and objects?.

//it's wrong
Map<String,?>params=new HashMap<String,? >
List<Person> lstperson=getPerson();
params.put("person",lstperson);
params.put("doc",objectDoc);
params.put("idSol",new Long(5));
service.method(params);

//method

public void method(Map<String, ?> params);

Answer

Dirk picture Dirk · Dec 18, 2013

Declare the hash map as

Map<String,Object> params = new HashMap<String,Object>();

You can keep the declaration of

public void method(Map<String, ?> params);

as it is, as long as the method only every tries to read from the map.