Java Generics question: parameter type E is not within its border

I have a question about generics. I have this method that does not compile at all. The compiler tells me: type parameter E is not within its bound . Usually I don’t understand the problems with the compiler, but it is rather complicated. Maybe I need to improve my knowledge of generics. :-) Can someone tell me what is wrong?

 public static <E extends Enum & StringConvertableEnum<E>> Map<String, E> map(Class<E> enumClass) { Map<String, E> mapping = new HashMap<String, E>(); EnumSet<E> set = EnumSet.allOf(enumClass); for(E enumConstant : set) { mapping.put(enumConstant.getStringValue(), enumConstant); } return mapping; } 

This is the definition of StringConvertableEnum :

 public interface StringConvertableEnum<E extends Enum> { public E getEnumFromStringValue(String string); public String getStringValue(); } 
+8
java generics
source share
1 answer

You need to change your ad to E extends Enum<E>

Change, sorry, I had to back down, I mean the full announcement:

 public static <E extends Enum<E> & StringConvertableEnum<E>> Map<String, E> map(Class<E> enumClass) { 
+9
source share

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


All Articles