Search code examples
javagenericshashmapjava-6java-5

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


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);

Solution

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