Converting Map<String,String> to Map<String,Object>

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


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


You can simply write

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