简体   繁体   中英

How to map enum to enum with the same keys

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

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)

tl;dr

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 is a data type

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.

No sharing between enums

So your question does not really make sense, given the definition of an enum. Objects of one enum are not shared with another enum.

Collections

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 ) ;

Member variable

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() ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM