Search code examples
javafilterdictionaryclone

Java: generic filter map function


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.


Solution

  • The problem with this code is that the type system prevents you from putting an object into a Map whose 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 be Object, or Integer, or List<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 the Map. As an example, if you have this method:

    public static void breakMyMap(Map<?, ?> m) {
        m.put(new Object(), new Object()); // Won't compile
    }
    

    and then write code like this:

    Map<String, String> myMap = new HashMap<String, String>();
    breakMyMap(myMap);
    

    Then if the code in breakMyMap were to compile, it would put a pair of Objects as keys and values into a Map<String, String>, breaking the invariant that all the elements are indeed Strings.

    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:

    public static <K, V> Map<K, V> filterAttrs(Map<K, V> args, String... unless) {
    
        Map<K, V> filteredAttrs = new HashMap<K, V>();
    
        Arrays.sort(unless);
        for (K o : args.keySet()) {
            String attr = o.toString();
            if (Arrays.binarySearch(unless, o.toString()) < 0 ) {
                filteredAttrs.put(o, args.get(o));
            }
        }
        return filteredAttrs;
    }
    

    Now that the compiler knows that the key type is K, it can verify that the put will 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

    Map<?, ?> filteredAttrs = Map.class.newInstance();
    

    Will cause an exception at runtime because Map is an interface, not a class, and so trying to use newInstance to 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:

    Map<K, V> filteredAttrs = args.getClass().newInstance();
    

    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!