Convert One Enum to Another in Java
This tutorial discusses how to convert one enum to another enum in Java. We will explore various approaches to achieve this conversion, depending on the requirements and the structure of the enums involved. We will provide code examples to illustrate each method.
1. Introduction
Enums in Java are a convenient way to represent a fixed set of constants. They provide type safety and can be used in switch statements and collections. However, there may be scenarios where you need to convert one enum type to another. This could be due to changes in the domain model or the need to map values between different enum types. In this tutorial, we will explore different techniques to perform enum conversions in Java.
2. Convert Enums with Matching Names
If both enums have matching names, you can use the valueOf()
method provided by Java’s enum class to convert between them. The valueOf()
method returns the enum constant with the specified name. Here’s an example:
enum Color { RED, GREEN, BLUE } enum RGB { RED, GREEN, BLUE } public class EnumConversionExample { public static void main(String[] args) { Color color = Color.RED; RGB rgb = RGB.valueOf(color.name()); System.out.println(rgb); } }
In this example, we have two enum types, Color
and RGB
, both with the same constant names. We convert a Color
enum constant to an RGB
enum constant by calling RGB.valueOf(color.name())
. The name()
method returns the name of the enum constant.
3. Convert Enums with Different Names
If the enums have different names or the mapping between the two is not straightforward, you can create a mapping mechanism to perform the conversion. This can be achieved using a Map
or a switch statement. Let’s look at an example using a Map
:
import java.util.HashMap; import java.util.Map; enum Color { RED, GREEN, BLUE } enum PrimaryColor { RED, YELLOW, BLUE } public class EnumConversionExample { public static void main(String[] args) { Map colorMap = new HashMap<>(); colorMap.put(Color.RED, PrimaryColor.RED); colorMap.put(Color.GREEN, PrimaryColor.YELLOW); colorMap.put(Color.BLUE, PrimaryColor.BLUE); Color color = Color.GREEN; PrimaryColor primaryColor = (PrimaryColor) colorMap.get(color1); System.out.println(primaryColor); } }
In this example, we create a Map
called colorMap
to map Color
enum constants to PrimaryColor
enum constants. We populate the map with the corresponding mappings. To perform the conversion, we retrieve the value from the map using the Color
enum constant as the key.
4. Convert Enums with Custom Mapping Logic
In some cases, the conversion logic between two enums may not be a simple one-to-one mapping. You may need to define custom logic to handle such conversions. One approach is to add a method in the source enum that returns the converted value. Here’s an example:
enum Size { SMALL, MEDIUM, LARGE; public Weight convertToWeight() { switch (this) { case SMALL: return Weight.LIGHT; case MEDIUM: return Weight.MEDIUM; case LARGE: return Weight.HEAVY; default: throw new IllegalArgumentException("Unsupported size: " + this); } } } enum Weight { LIGHT, MEDIUM, HEAVY } public class EnumConversionExample { public static void main(String[] args) { Size size = Size.SMALL; Weight weight = size.convertToWeight(); System.out.println(weight); } }
In this example, the Size
enum has a method called convertToWeight()
, which returns the corresponding Weight
enum constant based on the size. The conversion logic is implemented using a switch statement.
5. Convert Enums Using Interfaces
Another approach to convert enums is by using interfaces. You can define a common interface that both enums implement, with a method that performs the conversion. Here’s an example:
interface Convertible { T convert(); } enum Distance implements Convertible { METER { public Length convert() { return Length.METER; } }, KILOMETER { public Length convert() { return Length.KILOMETER; } } } enum Length { METER, KILOMETER } public class EnumConversionExample { public static void main(String[] args) { Distance distance = Distance.METER; Length length = distance.convert(); System.out.println(length); } }
In this example, we define an interface called Convertible
with a type parameter T
. The Distance
enum implements this interface, specifying Length
as the type parameter. Each enum constant in Distance
provides its implementation of the convert()
method.
6. Conclusion
Converting one enum to another can be achieved in different ways, depending on the requirements and the structure of the enums involved. In this tutorial, we explored several approaches, including using valueOf()
for enums with matching names, creating a mapping mechanism with a Map
or switch statement, defining custom mapping logic within the source enum, and using interfaces. Choose the approach that best suits your needs and ensures type safety and maintainability in your code.
7. Download the Source Code
This was an example of how to convert one enum to another enum in Java.
You can download the full source code of this example here: Convert One Enum to Another in Java