Here's a minimal example of the code I'm working with:
public class Temp {
enum SomeEnum {}
private static final Map<SomeEnum, String> TEST = new EnumMap<>(
Arrays.stream(SomeEnum.values())
.collect(Collectors.toMap(t -> t, a -> "")));
}
The compiler output is:
Temp.java:27: error: cannot infer type arguments for EnumMap<>
private static final Map<SomeEnum, String> TEST = new EnumMap<>(Arrays.stream(SomeEnum.values())
^
I have found that this can be worked around by replacing t -> t
with Function.identity()
or (SomeEnum t) -> t
, but I'm not understanding why this is the case. What limitation in javac is causing this behavior?
I originally found this issue with java 8, but have verified it still occurs with the java 11 compiler.
Supplier
explicitlynew EnumMap<>( Arrays.stream(SomeEnum.values()) .collect(Collectors.toMap(t -> t, a -> "", (x, y) -> x, () -> new EnumMap<>(SomeEnum.class))));
. References from Holger's answer – ChapaMake sure you test with javac, not the eclipse compiler. Eclipse compiler compiles this just fine.
– Faunia(SomeEnum t) -> t
). I give it the help it requires and all is well. – SymbolismHashMap<>(....
it will work – JacobiEnumMap(Map<K, ? extends V> m)
compared toHashMap(Map<? extends K, ? extends V> m)
.. could the key type be the issue? – Chapa