Reputation: 177
How to map enum to enum with the same keys in Java. For example..
public enum FirstEnum {
A, B
}
public enum ScndEnum {
A, B, C
}
I couldnt find good solution
Upvotes: 4
Views: 7708
Reputation: 4166
You can use Mapstruct library. It will automatically generate all needed code for you. You just need to wrote:
@Mapper
public interface MyMapper {
FirstEnum map(SecondEnum e);
}
And if values of both match, they will work by default (if not, you can create custom mapping).
Upvotes: 0
Reputation: 340350
Sharing objects between enums does not really make sense, given the definition of enums in Java.
Perhaps the solution for you is a collection of enums, such as Map
(key-value pairs) where the key type is of one enum class and the value type is of another enum class.
Map< Animal , Food > feeding =
Map.of
(
DOG , MEAT ,
CAT , MEAT ,
BIRD , SEED ,
BAT , FRUIT
)
;
An enum in Java is a class for which instances are named at compile-time, are automatically instantiated at run-time, and cannot be re-assigned. See tutorial by Oracle.
Examples:
enum Animal { DOG , CAT , BIRD , BAT ; }
enum Flavor { VANILLA , CHOCOLATE , LAVENDER , LEMON ; }
Each name within the enum will be assigned an object, an instance of that enum’s class. In the examples above, DOG
constant holds an Animal
object, while LEMON
constant holds a Flavor
object.
Using an enum in Java makes sense when you have a specific collection of instances that are known at compile-time and are unchanged at runtime. This means you cannot call new Animal()
in our example above. Four new Animal
objects were automatically instantiated and assigned to each of the variable names when the class first loaded. No more Animal
objects can be instantiated nor discarded after that enum class loads.
So your question does not really make sense, given the definition of an enum. Objects of one enum are not shared with another enum.
Perhaps what you are looking for is a collection of enum objects.
Set
For example, here we use the implementation of Set
that is highly optimized for holding enum objects: EnumSet
.
Set< Animal > furryPets = EnumSet.of( DOG , CAT ) ;
Set< Animal > flyingAnimals = EnumSet.of( BIRD , BAT ) ;
Set< Animal > featheredFlyingAnimals = flyingAnimals.clone().remove( BAT ) ;
Map
You mentioned wanting a two-way mapping. So perhaps what you want is a Map
to track one enum as a key and another enum as a value. The EnumMap
class is highly-optimized for when your key is an enum.
As an example, let's add another enum.
enum Food { MEAT , SEED , FRUIT ; }
Map an animal to its type of food.
Map< Animal , Food > feeding = new EnumMap<>( Animal.class ) ;
feeding.put( DOG , MEAT ) ;
feeding.put( CAT , MEAT ) ;
feeding.put( BIRD , SEED ) ;
feeding.put( BAT , FRUIT ) ;
Retrieve the type of food to feed to bats.
Food foodForBats = feeding.get( BAT ) ;
An enum in Java is a special kind of class, but still a class. So an enum can have member fields, constructors, and accessor methods, just like regular Java classes.
So we could associate an Animal
with its Food
this way, by using member fields, constructors, and accessor methods.
enum Animal
{
DOG( Food.MEAT ) ,
CAT( Food.MEAT ) ,
BIRD( Food.SEED ) ,
BAT( Food.FRUIT ) ;
// Member fields.
private Food food ;
// Constructor.
private Animal( Food foodArg )
{
this.food = foodArg ;
}
// Accessor "getter" method.
public Food getFood() { return this.food ; }
}
As mentioned above, when the Animal
class loads, its constructor is called four times, once per named constant. The specified Food
object is passed to that constructor automatically, on your behalf. You can think of that line DOG( Food.MEAT ) ,
as being an abbreviation of public static final Animal DOG = new Animal( Food.MEAT) ;
.
Usage.
Food foodForBats = Animal.BAT.getFood() ;
Upvotes: 2
Reputation: 45339
You can't map directly from one enum type to another, but you can use the value of name()
and map it using valueOf()
. Both of these methods come with all enum classes:
ScndEnum aToA = ScndEnum.valueOf(FirstEnum.A.name());
And if you want to make that generic:
private static <E extends Enum<E>, F extends Enum<F>>
F mapEnum(E enum1, Class<F> enum2Class) {
return Enum.valueOf(enum2Class, enum1.name());
}
Which you can call using something like
ScndEnum b = mapEnum(FirstEnum.B, ScndEnum.class)
Upvotes: 6