47

I have Two Maps

Map<String, String> filterMap 
Map<String, Object> filterMapObj

What I need is I would like to convert that Map<String, String> to Map<String, Object>. Here I am using the code

        if (filterMap != null) {
            for (Entry<String, String> entry : filterMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                Object objectVal = (Object)value;
                filterMapObj.put(key, objectVal);
            }
        }

It works fine, Is there any other ways by which I can do this without iterating through all the entries in the Map.

arjuncc
  • 3,227
  • 5
  • 42
  • 77

4 Answers4

55

Instead of writing your own loop that calls put, you can putAll, which does the same thing:

filterMapObj.putAll(filterMap);

(See the Javadoc.)

And as Asanka Siriwardena points out in his/her answer, if your plan is to populate filterMapObj immediately after creating it, then you can use the constructor that does that automatically:

filterMapObj = new HashMap<>(filterMap);

But to be clear, the above are more-or-less equivalent to iterating over the map's elements: it will make your code cleaner, but if your reason for not wanting to iterate over the elements is actually a performance concern (e.g., if your map is enormous), then it's not likely to help you. Another possibility is to write:

filterMapObj = Collections.<String, Object>unmodifiableMap(filterMap);

which creates an unmodifiable "view" of filterMap. That's more restrictive, of course, in that it won't let you modify filterMapObj and filterMap independently. (filterMapObj can't be modified, and any modifications to filterMap will affect filterMapObj as well.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • @PrasadKharkar: I wouldn't say that my solution is better than yours, just that they serve different purposes. (And yours hints that the OP may have an XY problem.) Nothing wrong with having both. :-) – ruakh Jan 10 '14 at 06:31
  • Hi @ruakh, why do we have to return an unmodifiableMap for the casting to work ? Can't java assert that a collection of String, String for instance can be returned as a collection of String, Object ? – Antonin Apr 04 '18 at 20:35
  • 2
    @Antonin: For a "cast" from `Map` to `Map` to be safe, *something* needs to make sure that the `put` method never adds inappropriate mappings to the original map (keeping in mind that the original map is very likely to be an instance of something like `HashMap` that doesn't do runtime type-checking). `unmodifiableMap` is not the only way to accomplish that, but it's among the simplest (and, I daresay, usually the most appropriate). – ruakh Apr 05 '18 at 01:06
  • @asanka's answer is straightforward: `Map filterMapObj = new HashMap<>(filterMap);` – Philippe Nov 17 '20 at 19:34
  • @Philippe: Good point. I'll add something about that. – ruakh Nov 18 '20 at 01:17
  • With static import and the deduction of generic types using `Collections` just looks like: `final Map filterMapObj = unmodifiableMap(filterMap);`; which is short, clear and, as my understanding, no performance penalty. I think that's the best approach – Javier Mr Jan 20 '21 at 15:41
10

You can use the wildcard operator for this. Define filterMapObj as Map<String, ? extends Object> filterMapObj and you can directly assign the filterMap to it. You can learn about generics wildcard operator

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
10

You can simply write

Map<String, Object> filterMapObj = new HashMap<>(filterMap);
Asanka Siriwardena
  • 871
  • 13
  • 18
1

You can use putAll method to solve the problem.The Object is the father class of all objects,so you can use putAll without convert.