Using Java 8, I have a Map that I need to convert to another Map replacing the key names and sometimes the value. E.g., when they turn out to be enums in which case I need to convert these enums to other constants (Strings and sometimes ints). I do not want to compare Strings, i.e. theEnum.name()
, due to possible duplicates but would prefer to convert the Object to an enum and switch on it. However, I fail to find a way to convert the Object to a switchable enum. Enum.valueOf
does not return something that can be switched upon (see the example below).
private void put(String key, Object value) {
if (value != null && value.getClass().isEnum()) {
Enum<?> val = (Enum<?>)value;
/* The below line has the following problem:
* Cannot switch on a value of type Enum.
* Only convertible int values, strings or enum variables are
* permitted */
switch (Enum.valueOf(val.getClass(), val.name())) {
case EmploymentType.FULL_TIME_EMPLOYEE : value = "Fast anställd";
break;
case ResidenceType.TENANCY : value = "Hyresrätt";
break;
default : break;
}
}
map.put(key, value);
}
I am aware that I could do:
private void put(String key, Object value) {
if (value != null && value.getClass().isEnum()) {
if (value.equals(EmploymentType.FULL_TIME_EMPLOYEE) value = "Fast anställd";
else if (value.equals(ResidenceType.TENANCY) value = "Hyresrätt";
}
map.put(key, value);
}
But is find that to be much less elegant and as easy to read as a switch statement would be. Is there a way to do that?
Yes, You can use Enum in Switch case statement in Java like int primitive. If you are familiar with enum int pattern, where integers represent enum values prior to Java 5 then you already knows how to use the Switch case with Enum.
We can use enums in C for multiple purposes; some of the uses of enums are: To store constant values (e.g., weekdays, months, directions, colors in a rainbow) For using flags in C. While using switch-case statements in C.
With the switch statement you can use int, char or, enum types. Usage of any other types generates a compile time error.
It looks like you have to deal with multiple enum types. But you can't switch accross multiple enum types in one switch statement. I think you could just use a Map<Enum<?>, Object>
instead.
You could setup an HashMap
like this:
Map<Enum<?>, Object> enumMap = new HashMap<>();
enumMap.put(EmploymentType.FULL_TIME_EMPLOYEE, "Fast anställd");
enumMap.put(ResidenceType.TENANCY, "Hyresrätt");
and use it like this:
if (enumMap.containsKey(value)) {
value = enumMap.get(value);
}
map.put(key, value);
One possibility would be to have the enums implement an interface with a getValue()
or similar method. Even if it were possible to switch between different enums, that code isn't very elegant.
Then you could just write
if (value != null && value instanceof MyInterface) {
MyInterface foo = (MyInterface) value;
value = foo.getValue(); // Although it's a bit ugly to reassign a parameter
}
map.put(key, value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With