I’m trying to develop a generic function to filter maps.
The code I have so far is:
public static Map<?, ?> filterAttrs(Map<?, ?> args, String... unless) {
Map<?, ?> filteredAttrs = Map.class.newInstance();
Arrays.sort(unless);
for (Object o : args.keySet()) {
if (Arrays.binarySearch(unless, o.toString()) < 0 ) {
filteredAttrs.put(o, args.get(o));
}
}
return filteredAttrs;
}
I get the following errors in filteredAttrs.put
The method put(capture#5-of ?, capture#6-of ?) in the type Map is not applicable for the arguments (Object, capture#8-of ?)
I don’t know how to instantiate a generic Map (I tried with 1Map.class.newInstance()`).
Any ideas?
Edit: after reading many answers, the problem seems to be how to make filteredAttrs be an instance of the same type as args. (Map) args.getClass().newInstance() seems to do the trick.
The problem with this code is that the type system prevents you from putting an object into a
Mapwhose key type is?. This is because if the key type is?, the compiler has no idea what’s actually being stored in the map – it could beObject, orInteger, orList<Object>– and so it can’t confirm that what you’re trying to add to the map is actually of the correct type and won’t be out of place in theMap. As an example, if you have this method:and then write code like this:
Then if the code in
breakMyMapwere to compile, it would put a pair ofObjects as keys and values into aMap<String, String>, breaking the invariant that all the elements are indeedStrings.To fix this, instead of making this function work on
Map<?, ?>, change the function so that you have more type information about what the keys and values are. For example, you could try this:Now that the compiler knows that the key type is
K, it can verify that theputwill not mix up the types of the keys in the map.Another thing I should point out is that the code you had would never have worked even if this did compile. The reason is that the line
Will cause an exception at runtime because
Mapis an interface, not a class, and so trying to usenewInstanceto create an instance of it will fail to work correctly. To fix this, you can either specify the type of the map explicitly (as I’ve done in the above code), or get the class of the argument:Of course, this isn’t guaranteed to work either, though the general contract for collections is that all collections should have a no-arg constructor.
Hope this helps!