'In a Java interface, is there a way to return an enum nested in a generic type?
I'm looking for a way to return an Enum type nested in a Generic class. Here's what I mean:
public interface MapperClass<E, D> {
D entityToDto(E entity);
default List<D> entitiesToDto(List<E> entities) {
return entities.stream()
.map(this::entityToDto)
.collect(Collectors.toList());
}
D.Enum mapString(String str);
}
In other words, my method mapString()
should return an enum that is nested in the generic type D
, is there a way to accomplish this in Java?
Solution 1:[1]
May be something like this:
public interface MapperClass<E extends Enum<Test.TestEnum>, D extends Test> {
E mapString(String a);
..
}
Your Test
Class:
public class Test {
..
enum TestEnum{
A,B,C; //for demonstration purpose
}
..
}
Interface implementation example :
MapperClass<Enum<Test.TestEnum>, Test> a=new MapperClass<Enum<Test.TestEnum>, Test>() {
..
@Override
public Enum<Test.TestEnum> mapString(String a) {
return Test411.A;
}
..
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ashish Patil |