Enumeration types

  • Enumeration types
  • Use Enum type to define constants and use these to loop through

Enumeration types

Enumeration types are special classes that define constants. These constants have a numeric value, meaning that you can use them e.g. in a switch statement. On top of that, each constant value of a enum class can have attributes, which can be accessed by e.g. getter functions.

/**
 * Name of different studies
 */
public enum Studies {
    
    MATHEMATICS("Mathematics", 160.0),
    
    COMPUTER_SCIENCE("Computer science", 120.0);

    private String name;
    private double numberStudyCredits;

    /**
     * Default constructor.
     *
     * @param name study name
     * @param numberStudyCredits amount of credits
     */
    private Studies(String name, double numberStudyCredits){
        this.name = name;
        this.numberStudyCredits = numberStudyCredits;

    }

    public String getName(){
        return name;
    };

    public double getNumberStudyCredits(){
        return numberStudyCredits;
    };
}

public class MyApp{
  public static main(String[] arg){

    for (Studies study : Studies.values()){
          System.out.println(study.getName());
    }
  }
}

Use Enum type to define constants and use these to loop through


// Define 1) Enum class with constants and 2) static method returning an array of this enum type based on string split 
public enum StatsType {
    MIN, NBASES;
   
    public static StatsType[] conversionMethod(String statsTypeNames ){
        StatsType[] statsTypes;
        String[] splittedStatsTypeNames = statsTypeNames.split(",");

        statsTypes = new StatsType[splittedStatsTypeNames.length];
        
        int index = 0;
        for(String typeName : splittedStatsTypeNames){
            StatsType type = StatsType.valueOf(typeName);
            statsTypes[index] = type;
            index++;
        }
        return statsTypes;
    }

// Define class that executes behavior depending on enum
public class StatsCalculator{
    private final StatsType[] statsTypes;

    /**
    @param typesString comma-separated string with StatsType enums
    **/
    public StatsCalculator(String typesString) {
            this.statsTypes =  RunTool.conversionMethod(typesString);
        }
    public run(){
        for (StatsType type : statsTypes){
            execute(type);

        }
    }
    private execute(StatsType type){
        switch type{
            case MIN:
                doMin();
                break;
            case MEAN:
                doMean();
                break;
            default;
                break;
        }
    }
}   


// Define class with main function that accepts a comma-separated string as input and instantiates the previous class with its behavior
public class MyApp() {
    public static void main(String[] args){
        OptionsSet options = OptionsParser.parse(args);
        StatsType[] types = StatsType.conversionMethod(options.valueOf("types"));
        StatsCalculator sc = new StatsCalculator(types);
        sc.run();
    }
        
}

Useful Examples

// Save all values of enum as list
List<String> enumNames = Stream.of(Enum.values()).map(Enum::name).collect(Collectors.toList());
// same
EnumSet.allOf(MyEnum.class).stream().map(e -> e.name()).collect(Collectors.toList())