开发者

Java: generic filter map function

开发者 https://www.devze.com 2023-02-12 09:49 出处:网络
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) {

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();

 开发者_JAVA技巧   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 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!


I consider it to be bad if people are reinventing the wheel (and not doing any better).

To fix all your problems in a single suggestion: Can you try Google Guava?

For example then you could use Maps:

  • Maps.filterEntries
  • Maps.filterKeys
  • Maps.filterValues

Or at least write a filter that takes predicate as argument.


This line

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

should throw an InstantiationException - because Map is an interface - unless you've implemented your own class named Map


You cannot put anything into a Map<?, ?>, because the compiler doesn't know exactly what kind of map it is. In the line:

filteredAttrs.put(o, args.get(o));

you are pretending that the Map<?, ?> is a Map<Object, Object>, but that's not what it is. Angelika Langer's Java Generics FAQ explains why this doesn't work in more detail.

You should add type parameters to the method; then it's also not necessary to create the new map via reflection:

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()) {
        if (Arrays.binarySearch(unless, o.toString()) < 0) {
            filteredAttrs.put(o, args.get(o));
        }
    }
    return filteredAttrs;
}


You cannot instantiate Map because it is an interface. Don't use ? because it causes confusion to begin with.

public static <T, K> Map<T, K> filterAttrs(Map<T, K> args, T... unless) {
    Map<T, K> filteredAttrs;

    filteredAttrs = new HashMap<T, K>();
    Arrays.sort(unless);
    for (T o : args.keySet()) {
        if (Arrays.binarySearch(unless, o) < 0) {
            filteredAttrs.put(o, args.get(o));
        }
    }

    return filteredAttrs;
}

You have this line that does nothing, so I removed it:-

String attr = o.toString();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号