How to convert HashMap<String,Integer> param to Map<String,Object)

14,830

Solution 1

Simply use the constructor taking another map as an argument:

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

See this example:

import java.util.HashMap;
import java.util.Map;

public class Test5 {

    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("1", 1);
        Map<String, Object> map2 = new HashMap<String, Object>(map);
        // etc...
    }
}

Solution 2

If you have a HashMap<String,Integer> and you need to convert it to a HashMap<String,Object>, then the following should work:

HashMap<String, Object> objParams = new HashMap<String, Object>();
for (String key : intParams.keyValues()) {
    Integer intValue = intParams.get(key);
    objParams.put(key, intValue);
}

Where the intParams is your HashMap<String,Integer>.

There might be some typos in there as this is purely off the cuff.

Then you can pass the objParams to fillReport.

Share:
14,830
Ali Asad Sahu
Author by

Ali Asad Sahu

Updated on June 04, 2022

Comments

  • Ali Asad Sahu
    Ali Asad Sahu almost 2 years

    I want to call JasperFillManager.fillReport(filePath+".jasper", param, con); where param is supposed to accept type Map. is there any solution

  • Guillaume Polet
    Guillaume Polet almost 12 years
    When iterating over a Map, consider iterating over the entrySet() instead of the keySet(). It is much more efficient as it does not requires the hashCode() and equals() methods of the key to be evaluate everytime to find the corresponding value.