IMHO, In general, Java avoids type inference.
In any case, <> only works when the compiler does not need to know which generic type was used. In the case of anonymous classes, the actual type must be provided, since the compiler does not infer the type.
Effectively <> disables type checking, and does not provide type inference. An anonymous class stores the actual generic type and therefore you must provide it.
List<String> a = new ArrayList<>()
rather like
@SuppressWarnings("unchecked") List<String> a = new ArrayList()
but for an anonymous subclass, the compiler must give it a generic type.
source share