Your code will work if you change the signature of the card in the calling method:
No change required for make
public static <A, B> Map<A, B> make(final A key1, final B value1, final A key2, final B value2) { Map<A, B> map = new HashMap<A, B>(); map.put(key1, value1); map.put(key2, value2); return map; }
make caller
Should you change Map<String, Object> to Map<String, ? extends Object> Map<String, ? extends Object> :
public static void main(String[] args) { @SuppressWarnings("unused") Map<String, ? extends Object> m = make("a", new Integer(1), "2", "efg"); }
EDIT1: OpenJDK 1.6 + Eclipse Indigo Compiler
EDIT2: When creating such community cards, you have to accept that you need to downgrade when it comes to getting values.
EDIT3: Is there a way to fix this? Of course, defining a function with a signature of make (Object ... keysAndValues) would work, but I would lose the ability to compile time types.
At some point, you will always lose compile-time security. At least when it comes to extraction.
source share