In my project I have an Enum like this :
public enum MyEnum {
FIRST(1),
SECOND(2);
private int value;
private MyEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static MyEnum fromValue(int value) {
for (MyEnum e : MyEnum.values()) {
if (e.getValue() == value) {
return e;
}
}
return null;
}
And I have this code :
Map<String, Object> myMap = new HashMap<>();
// Fill my Map with data from database
myMap = namedParameterJdbcTemplate.queryForList(qry, paramsMap);
***if (Arrays.asList(MyEnum.values())
.contains(MyEnum.fromValue((int)
myMap.get("myKey")) )))*** {
// Do something
}
I get the Exception
java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Integer** on this line : **if (Arrays.asList(MyEnum.values())
.contains(MyEnum.fromValue((int)myMap.get("myKey")) )))
myMap is filled with data from database, knowing that it is an SQL Server Database and that the myKey returned from database is of type tinyint in the database.
Can you please tell me what I am doing wrong ? Thanks.
Regards.
EnumSet.allOf(MyEnum.class)is far more efficient (and easier to read) than Arrays.asList(MyEnum.values()). See the documentation for more information.