You will need to have your enums implement a common interface like so:
import java.util.*;
public class Zoo {
private static final Map<String, ZooAnimal> animals;
static {
Map<String, ZooAnimal> tmp = new HashMap<>();
tmp.put("CHEETAH", Cat.CHEETAH);
tmp.put("LION", Cat.LION);
tmp.put("PANTHER", Cat.PANTHER);
tmp.put("TIGER", Cat.TIGER);
tmp.put("HAWK", Bird.HAWK);
tmp.put("OSTRICH", Bird.OSTRICH);
tmp.put("OWL", Bird.OWL);
animals = Collections.unmodifiableMap(tmp);
}
public static void main(String[] args) {
ZooAnimal animal = animals.get("PANTHER");
if (animal != null && animal instanceof Cat) {
// A panther is a cat
System.out.printf("A %s is a cat%n", animal.getName().toLowerCase());
}
}
private interface ZooAnimal {
String getName();
}
public enum Cat implements ZooAnimal {
CHEETAH, LION, PANTHER, TIGER;
@Override
public String getName() {
return this.name();
}
};
public enum Bird implements ZooAnimal {
HAWK, OSTRICH, OWL;
@Override
public String getName() {
return this.name();
}
};
}
If you want the values, you can use the generic Enum
class:
import java.util.*;
public class Zoo {
private static final Map<String, Class<? extends Enum<?>>> animals;
static {
Map<String, Class<? extends Enum<?>>> tmp = new HashMap<>();
tmp.put("BIRD", Bird.class);
tmp.put("CAT", Cat.class);
animals = Collections.unmodifiableMap(tmp);
}
public static void main(String[] args) {
Class<? extends Enum<?>> animal = animals.get("CAT");
// Print the values
for (Enum<?> c : animal.getEnumConstants()) {
System.out.println(c.name()); // CHEETAH, LION, PANTHER, TIGER
}
}
public enum Cat {
CHEETAH, LION, PANTHER, TIGER;
};
public enum Bird {
HAWK, OSTRICH, OWL;
};
}