javajakarta-ee

Create a HashSet By Enumeration


I have class :

@Entity
@Table(name = "PROJECT_INDICATOR")
public class Project_Indicator {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @NotNull
    @ManyToOne
    private Indicator indicator;
    @NotNull
    @ManyToOne
    private Project project;
    @NotNull
    private int year;
    // here I want create List (HashSet) with enumeration of months
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public Indicator getIndicator() {
        return indicator;
    }
    public void setIndicator(Indicator indicator) {
        this.indicator = indicator;
    }
    public Project getProject() {
        return project;
    }
    public void setProject(Project project) {
        this.project = project;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }

}

This Project_Indicator class must contain an attribute wish is a List of months (jan until december) I want create a list with keys are (jan until dec) and each month have an attribute for example int value, then I want to do this :

int value=mySet.get("July");

please just I want an idea (or other Idea) if you understood my problem.

Thanx.


Solution

  • java.time.Month enum

    If you are able to use Java 8 or later, use the new Date and Time API. Specifically, you will find the new Month enum useful:

    Month month = Month.JULY;
    int value = month.getValue();   // value == 7 for July, 1-12 for January-December. 
    

    EnumMap

    If you would like your own month / value association you can use an EnumMap optimized for enum keys:

    Map<Month, Integer> map = EnumMap<>(Month.class);
    map.put(Month.JULY, 42);