50

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.

1
  • Why not use the putAll api method, in case you want to keep both the references. Commented Jan 10, 2014 at 6:18

4 Answers 4

57

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

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

6 Comments

@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. :-)
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: For a "cast" from Map<String, String> to Map<String, Object> 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).
@asanka's answer is straightforward: Map<String, Object> filterMapObj = new HashMap<>(filterMap);
@Philippe: Good point. I'll add something about that.
|
16

You can simply write

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

2 Comments

This is by far the simplest answer. I would vote for it being "most correct."
This was the one that worked for me, and solved the problem in just one line of code.
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.