Java: general filter mapping function

I am trying to develop a general function for filtering 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 filterAttrs.put file

The put (capture # 5-of ?, capture # 6-of?) Method in the Map type is not applicable for arguments (Object, capture # 8-of?)

I do not know how to create an instance Map(I tried with 1Map.class.newInstance () `).

Any ideas?

Edit: after reading many answers, the problem is how to make an filteredAttrsinstance of the same type as args. (Map) args.getClass().newInstance()seems to be doing the trick.

+3
source share
5 answers

, Map, ?. , ?, , - Object Integer List<Object> - , , , Map. , :

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

:

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

, breakMyMap , Object Map<String, String>, , String s.

, , Map<?, ?>, , , . , :

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;
}

, , K, , put .

, , , , , , . ,

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

, Map - , , newInstance . , ( ) :

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

, , , no-arg.

, !

+8

, , ( ).

: Google Guava?

, Maps:

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

, , , .

+5

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

InstantiationException, Map - Map

+3

- Map<?, ?>, , . :

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

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

You should add type parameters to the method; there is also no need to create a new map through 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;
}
+3
source

You cannot create a map because it is an interface. Do not use ?because it leads to confusion.

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 deleted it: -

String attr = o.toString();
+2
source

Source: https://habr.com/ru/post/1793452/


All Articles