Your intent was to initialize an anonymous instance of HashMap. The warning is clue that your code is doing more than you intended.
What we're looking for is a way to initialize an anonymous HashMap instance. What we have above creates an anonymous subclass of HashMap then creates an anonymous instance of that anonymous class.
Because the code does more than was intended, I'd call it a hack.
What we really want is something like this:
foo(new HashMap<String, String>({"a", "value-a"}, {"c", "value-c"}));
But alas this isn't valid Java. There isn't a way to do anything this in a type-safe way using an array of key/value pairs. Java simple doesn't have the expressive power.
The Google Collection's ImmutableMap.of static methods are close but it means creating a version of the factory method for various numbers of key/value pairs. (See finnw's answer.)
So keep things simple. Go with Bart K's solution unless your code is littered with this initialization. If so use ImmutableMap. Or roll your own HashMap subclass with the "of" style factory methods. Or create these "of" style factory methods in a utility class. Here's one for two key/value pairs:
public final MapUtil {
public static <K,V> Map<K,V> makeMap(K k1, V v1, K k2, V v2) {
Map<K,V> m = new HashMap<K,V>();
m.put(k1, v1);
m.put(k2, v2);
return m;
}
}
Embrace the verbosity and take solace in the knowledge your corporate co-workers are wearing the same shackles as you.