Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make key value like enum in java [duplicate]

I need to make an Enum containing some strings with spaces and their values in int like:

public enum status{
Active(1),
Inactive(2);
}

because I am using it with hibernate and also will convert it to JSON for alpaca js forms.

like:

[{"text": "Inactive", "value":"2"},{"text": "Active", "value":"1"}]

I'm stuck in making enum. how to make such type of enum?

like image 544
Saqib Ahmed Avatar asked Nov 11 '16 11:11

Saqib Ahmed


People also ask

What is an enum in Java?

An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces). Why And When To Use Enums?

How to loop through the constants of an enum in Java?

The enum type has a values () method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum: An enum can, just like a class, have attributes and methods.

How do you find the value of an enum in Python?

values() method can be used to return all values present inside enum. Order is important in enums.By using ordinal() method , each enum constant index can be found, just like array index. valueOf() method returns the enum constant of the specified string value, if exists.

How to look up enum values by label Field in Java?

However, we may want to look up an enum value by our label field as well. To do that, we can add a static method: The static valueOfLabel () method iterates the Element values until it finds a match. It returns null if no match is found. Conversely, an exception could be thrown instead of returning null.


2 Answers

You can not put space between strings. Instead of the you can use underscore as follows:

In_Active

You can use this way:

enum Status {

    ACTIVE("Active", 1), IN_ACTIVE("In Active", 2);

    private final String key;
    private final Integer value;

    Status(String key, Integer value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }
    public Integer getValue() {
        return value;
    }
}
like image 166
Chandana Kumara Avatar answered Sep 24 '22 16:09

Chandana Kumara


You can hold multiple values in one enum and even have getters to handle them. Here is an example I used once (I try to adapt it to your problem):

public enum Status{

    ACTIVE(1, "Active"),
    INACTIVE(2, "In Active");

    private final Integer value;
    private final String text;

    /**
     * A mapping between the integer code and its corresponding text to facilitate lookup by code.
     */
    private static Map<Integer, Status> valueToTextMapping;

    private Status(Integer value, String text){
        this.value = value;
        this.text = text;
    }

    public static Status getStatus(Integer i){
        if(valueToTextMapping == null){
            initMapping();
        }
        return valueToTextMapping.get(i);
    }

    private static void initMapping(){
        valueToTextMapping = new HashMap<>();
        for(Status s : values()){
            valueToTextMapping.put(s.value, s);
        }
    }

    public Integer getValue(){
        return value;
    }

    public String getText(){
        return text;
    }

    @Override
    public String toString(){
        final StringBuilder sb = new StringBuilder();
        sb.append("Status");
        sb.append("{value=").append(value);
        sb.append(", text='").append(text).append('\'')
        sb.append('}');
        return sb.toString();
    }
}

So in your code you can simply use Status.ACTIVE and it will represent an instance of your Enum, that holds value and text the way you want it

like image 30
GameDroids Avatar answered Sep 21 '22 16:09

GameDroids