Java getting the Enum name given the Enum Value












44















How can I get the name of a Java Enum type given its value?



I have the following code which works for a particular Enum type, can I make it more generic?



public enum Category {

APPLE("3"),
ORANGE("1"),

private final String identifier;

private Category(String identifier) {
this.identifier = identifier;
}

public String toString() {
return identifier;
}

public static String getEnumNameForValue(Object value){
Category values = Category.values();
String enumValue = null;
for(Category eachValue : values) {
enumValue = eachValue.toString();

if (enumValue.equalsIgnoreCase(value)) {
return eachValue.name();
}
}
return enumValue;
}
}









share|improve this question




















  • 2





    Is there a special reason why you want to use something other than the name() of the Enum for looking them up? That would confuse a lot of people, and you also cannot use a simple Category.valueOf(name).

    – Thilo
    Oct 8 '10 at 9:36











  • Really can we make it more generic? I am using a lot of nameOf(String name), now for each I will write a *Enum.values().stream().filter(...).findAny().get(), which is so annoying.

    – Hearen
    May 18 '18 at 12:33
















44















How can I get the name of a Java Enum type given its value?



I have the following code which works for a particular Enum type, can I make it more generic?



public enum Category {

APPLE("3"),
ORANGE("1"),

private final String identifier;

private Category(String identifier) {
this.identifier = identifier;
}

public String toString() {
return identifier;
}

public static String getEnumNameForValue(Object value){
Category values = Category.values();
String enumValue = null;
for(Category eachValue : values) {
enumValue = eachValue.toString();

if (enumValue.equalsIgnoreCase(value)) {
return eachValue.name();
}
}
return enumValue;
}
}









share|improve this question




















  • 2





    Is there a special reason why you want to use something other than the name() of the Enum for looking them up? That would confuse a lot of people, and you also cannot use a simple Category.valueOf(name).

    – Thilo
    Oct 8 '10 at 9:36











  • Really can we make it more generic? I am using a lot of nameOf(String name), now for each I will write a *Enum.values().stream().filter(...).findAny().get(), which is so annoying.

    – Hearen
    May 18 '18 at 12:33














44












44








44


4






How can I get the name of a Java Enum type given its value?



I have the following code which works for a particular Enum type, can I make it more generic?



public enum Category {

APPLE("3"),
ORANGE("1"),

private final String identifier;

private Category(String identifier) {
this.identifier = identifier;
}

public String toString() {
return identifier;
}

public static String getEnumNameForValue(Object value){
Category values = Category.values();
String enumValue = null;
for(Category eachValue : values) {
enumValue = eachValue.toString();

if (enumValue.equalsIgnoreCase(value)) {
return eachValue.name();
}
}
return enumValue;
}
}









share|improve this question
















How can I get the name of a Java Enum type given its value?



I have the following code which works for a particular Enum type, can I make it more generic?



public enum Category {

APPLE("3"),
ORANGE("1"),

private final String identifier;

private Category(String identifier) {
this.identifier = identifier;
}

public String toString() {
return identifier;
}

public static String getEnumNameForValue(Object value){
Category values = Category.values();
String enumValue = null;
for(Category eachValue : values) {
enumValue = eachValue.toString();

if (enumValue.equalsIgnoreCase(value)) {
return eachValue.name();
}
}
return enumValue;
}
}






java enums enumeration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 18 '18 at 13:05









Hearen

2,8051428




2,8051428










asked Oct 8 '10 at 9:23









JuliaJulia

221134




221134








  • 2





    Is there a special reason why you want to use something other than the name() of the Enum for looking them up? That would confuse a lot of people, and you also cannot use a simple Category.valueOf(name).

    – Thilo
    Oct 8 '10 at 9:36











  • Really can we make it more generic? I am using a lot of nameOf(String name), now for each I will write a *Enum.values().stream().filter(...).findAny().get(), which is so annoying.

    – Hearen
    May 18 '18 at 12:33














  • 2





    Is there a special reason why you want to use something other than the name() of the Enum for looking them up? That would confuse a lot of people, and you also cannot use a simple Category.valueOf(name).

    – Thilo
    Oct 8 '10 at 9:36











  • Really can we make it more generic? I am using a lot of nameOf(String name), now for each I will write a *Enum.values().stream().filter(...).findAny().get(), which is so annoying.

    – Hearen
    May 18 '18 at 12:33








2




2





Is there a special reason why you want to use something other than the name() of the Enum for looking them up? That would confuse a lot of people, and you also cannot use a simple Category.valueOf(name).

– Thilo
Oct 8 '10 at 9:36





Is there a special reason why you want to use something other than the name() of the Enum for looking them up? That would confuse a lot of people, and you also cannot use a simple Category.valueOf(name).

– Thilo
Oct 8 '10 at 9:36













Really can we make it more generic? I am using a lot of nameOf(String name), now for each I will write a *Enum.values().stream().filter(...).findAny().get(), which is so annoying.

– Hearen
May 18 '18 at 12:33





Really can we make it more generic? I am using a lot of nameOf(String name), now for each I will write a *Enum.values().stream().filter(...).findAny().get(), which is so annoying.

– Hearen
May 18 '18 at 12:33












4 Answers
4






active

oldest

votes


















37














You should replace your getEnumNameForValue by a call to the name() method.






share|improve this answer





















  • 1





    @Jukia: Also, consider overriding toString(): download.oracle.com/javase/6/docs/api/java/lang/…

    – trashgod
    Oct 8 '10 at 11:58






  • 5





    @trashgod: She is overriding toString

    – Thilo
    Oct 9 '10 at 1:43











  • @Thilo: My error; thanks. @Julia: Sorry about misspelling your name.

    – trashgod
    Oct 9 '10 at 1:54



















25














Try below code



public enum SalaryHeadMasterEnum {

BASIC_PAY("basic pay"),
MEDICAL_ALLOWANCE("Medical Allowance");

private String name;

private SalaryHeadMasterEnum(String stringVal) {
name=stringVal;
}
public String toString(){
return name;
}

public static String getEnumByString(String code){
for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
if(code == e.name) return e.name();
}
return null;
}
}


Now you can use below code to retrieve the Enum by Value



SalaryHeadMasterEnum.getEnumByString("Basic Pay")


Use Below code to get ENUM as String



SalaryHeadMasterEnum.BASIC_PAY.name()


Use below code to get string Value for enum



SalaryHeadMasterEnum.BASIC_PAY.toString()





share|improve this answer





















  • 3





    code == e.name is not going to work the way you meant it to, should be e.name.equals(code) instead

    – f_puras
    Nov 22 '16 at 14:40











  • @f_puras did you test the above code??

    – prashant thakre
    Nov 22 '16 at 15:40






  • 4





    Well, yes: SalaryHeadMasterEnum.getEnumByString("basic pay") does work, but relies on the JVM merging String literals. Stuff like SalaryHeadMasterEnum.getEnumByString(new StringBuilder("basic ").append("pay").toString()) does not, but should IMHO. Using String.equals() would make both work ;-)

    – f_puras
    Nov 22 '16 at 21:46






  • 1





    - for comparing strings with == which is bad and for using a variable 'name' with a different meaning than the 'name()' method

    – Hardcoded
    Apr 27 '17 at 15:09





















5














Try, the following code..



    @Override
public String toString() {
return this.name();
}





share|improve this answer
























  • This should work well, but why call the function toString() instead of getName() (Or something similar) ?

    – Mayuso
    Oct 20 '16 at 11:15



















1














Here is the below code, it will return the Enum name from Enum value.



public enum Test {

PLUS("Plus One"), MINUS("MinusTwo"), TIMES("MultiplyByFour"), DIVIDE(
"DivideByZero");
private String operationName;

private Test(final String operationName) {
setOperationName(operationName);
}

public String getOperationName() {
return operationName;
}

public void setOperationName(final String operationName) {
this.operationName = operationName;
}

public static Test getOperationName(final String operationName) {

for (Test oprname : Test.values()) {
if (operationName.equals(oprname.toString())) {
return oprname;
}
}
return null;
}

@Override
public String toString() {
return operationName;
}
}

public class Main {
public static void main(String args) {

Test test = Test.getOperationName("Plus One");
switch (test) {
case PLUS:
System.out.println("Plus.....");
break;
case MINUS:
System.out.println("Minus.....");
break;

default:
System.out.println("Nothing..");
break;
}
}
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3889248%2fjava-getting-the-enum-name-given-the-enum-value%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    37














    You should replace your getEnumNameForValue by a call to the name() method.






    share|improve this answer





















    • 1





      @Jukia: Also, consider overriding toString(): download.oracle.com/javase/6/docs/api/java/lang/…

      – trashgod
      Oct 8 '10 at 11:58






    • 5





      @trashgod: She is overriding toString

      – Thilo
      Oct 9 '10 at 1:43











    • @Thilo: My error; thanks. @Julia: Sorry about misspelling your name.

      – trashgod
      Oct 9 '10 at 1:54
















    37














    You should replace your getEnumNameForValue by a call to the name() method.






    share|improve this answer





















    • 1





      @Jukia: Also, consider overriding toString(): download.oracle.com/javase/6/docs/api/java/lang/…

      – trashgod
      Oct 8 '10 at 11:58






    • 5





      @trashgod: She is overriding toString

      – Thilo
      Oct 9 '10 at 1:43











    • @Thilo: My error; thanks. @Julia: Sorry about misspelling your name.

      – trashgod
      Oct 9 '10 at 1:54














    37












    37








    37







    You should replace your getEnumNameForValue by a call to the name() method.






    share|improve this answer















    You should replace your getEnumNameForValue by a call to the name() method.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 15 '13 at 15:47









    thSoft

    15.6k57593




    15.6k57593










    answered Oct 8 '10 at 9:26









    RiduidelRiduidel

    18k1160134




    18k1160134








    • 1





      @Jukia: Also, consider overriding toString(): download.oracle.com/javase/6/docs/api/java/lang/…

      – trashgod
      Oct 8 '10 at 11:58






    • 5





      @trashgod: She is overriding toString

      – Thilo
      Oct 9 '10 at 1:43











    • @Thilo: My error; thanks. @Julia: Sorry about misspelling your name.

      – trashgod
      Oct 9 '10 at 1:54














    • 1





      @Jukia: Also, consider overriding toString(): download.oracle.com/javase/6/docs/api/java/lang/…

      – trashgod
      Oct 8 '10 at 11:58






    • 5





      @trashgod: She is overriding toString

      – Thilo
      Oct 9 '10 at 1:43











    • @Thilo: My error; thanks. @Julia: Sorry about misspelling your name.

      – trashgod
      Oct 9 '10 at 1:54








    1




    1





    @Jukia: Also, consider overriding toString(): download.oracle.com/javase/6/docs/api/java/lang/…

    – trashgod
    Oct 8 '10 at 11:58





    @Jukia: Also, consider overriding toString(): download.oracle.com/javase/6/docs/api/java/lang/…

    – trashgod
    Oct 8 '10 at 11:58




    5




    5





    @trashgod: She is overriding toString

    – Thilo
    Oct 9 '10 at 1:43





    @trashgod: She is overriding toString

    – Thilo
    Oct 9 '10 at 1:43













    @Thilo: My error; thanks. @Julia: Sorry about misspelling your name.

    – trashgod
    Oct 9 '10 at 1:54





    @Thilo: My error; thanks. @Julia: Sorry about misspelling your name.

    – trashgod
    Oct 9 '10 at 1:54













    25














    Try below code



    public enum SalaryHeadMasterEnum {

    BASIC_PAY("basic pay"),
    MEDICAL_ALLOWANCE("Medical Allowance");

    private String name;

    private SalaryHeadMasterEnum(String stringVal) {
    name=stringVal;
    }
    public String toString(){
    return name;
    }

    public static String getEnumByString(String code){
    for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
    if(code == e.name) return e.name();
    }
    return null;
    }
    }


    Now you can use below code to retrieve the Enum by Value



    SalaryHeadMasterEnum.getEnumByString("Basic Pay")


    Use Below code to get ENUM as String



    SalaryHeadMasterEnum.BASIC_PAY.name()


    Use below code to get string Value for enum



    SalaryHeadMasterEnum.BASIC_PAY.toString()





    share|improve this answer





















    • 3





      code == e.name is not going to work the way you meant it to, should be e.name.equals(code) instead

      – f_puras
      Nov 22 '16 at 14:40











    • @f_puras did you test the above code??

      – prashant thakre
      Nov 22 '16 at 15:40






    • 4





      Well, yes: SalaryHeadMasterEnum.getEnumByString("basic pay") does work, but relies on the JVM merging String literals. Stuff like SalaryHeadMasterEnum.getEnumByString(new StringBuilder("basic ").append("pay").toString()) does not, but should IMHO. Using String.equals() would make both work ;-)

      – f_puras
      Nov 22 '16 at 21:46






    • 1





      - for comparing strings with == which is bad and for using a variable 'name' with a different meaning than the 'name()' method

      – Hardcoded
      Apr 27 '17 at 15:09


















    25














    Try below code



    public enum SalaryHeadMasterEnum {

    BASIC_PAY("basic pay"),
    MEDICAL_ALLOWANCE("Medical Allowance");

    private String name;

    private SalaryHeadMasterEnum(String stringVal) {
    name=stringVal;
    }
    public String toString(){
    return name;
    }

    public static String getEnumByString(String code){
    for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
    if(code == e.name) return e.name();
    }
    return null;
    }
    }


    Now you can use below code to retrieve the Enum by Value



    SalaryHeadMasterEnum.getEnumByString("Basic Pay")


    Use Below code to get ENUM as String



    SalaryHeadMasterEnum.BASIC_PAY.name()


    Use below code to get string Value for enum



    SalaryHeadMasterEnum.BASIC_PAY.toString()





    share|improve this answer





















    • 3





      code == e.name is not going to work the way you meant it to, should be e.name.equals(code) instead

      – f_puras
      Nov 22 '16 at 14:40











    • @f_puras did you test the above code??

      – prashant thakre
      Nov 22 '16 at 15:40






    • 4





      Well, yes: SalaryHeadMasterEnum.getEnumByString("basic pay") does work, but relies on the JVM merging String literals. Stuff like SalaryHeadMasterEnum.getEnumByString(new StringBuilder("basic ").append("pay").toString()) does not, but should IMHO. Using String.equals() would make both work ;-)

      – f_puras
      Nov 22 '16 at 21:46






    • 1





      - for comparing strings with == which is bad and for using a variable 'name' with a different meaning than the 'name()' method

      – Hardcoded
      Apr 27 '17 at 15:09
















    25












    25








    25







    Try below code



    public enum SalaryHeadMasterEnum {

    BASIC_PAY("basic pay"),
    MEDICAL_ALLOWANCE("Medical Allowance");

    private String name;

    private SalaryHeadMasterEnum(String stringVal) {
    name=stringVal;
    }
    public String toString(){
    return name;
    }

    public static String getEnumByString(String code){
    for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
    if(code == e.name) return e.name();
    }
    return null;
    }
    }


    Now you can use below code to retrieve the Enum by Value



    SalaryHeadMasterEnum.getEnumByString("Basic Pay")


    Use Below code to get ENUM as String



    SalaryHeadMasterEnum.BASIC_PAY.name()


    Use below code to get string Value for enum



    SalaryHeadMasterEnum.BASIC_PAY.toString()





    share|improve this answer















    Try below code



    public enum SalaryHeadMasterEnum {

    BASIC_PAY("basic pay"),
    MEDICAL_ALLOWANCE("Medical Allowance");

    private String name;

    private SalaryHeadMasterEnum(String stringVal) {
    name=stringVal;
    }
    public String toString(){
    return name;
    }

    public static String getEnumByString(String code){
    for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
    if(code == e.name) return e.name();
    }
    return null;
    }
    }


    Now you can use below code to retrieve the Enum by Value



    SalaryHeadMasterEnum.getEnumByString("Basic Pay")


    Use Below code to get ENUM as String



    SalaryHeadMasterEnum.BASIC_PAY.name()


    Use below code to get string Value for enum



    SalaryHeadMasterEnum.BASIC_PAY.toString()






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 30 '16 at 13:47









    Riduidel

    18k1160134




    18k1160134










    answered Jun 15 '13 at 9:43









    prashant thakreprashant thakre

    3,50911934




    3,50911934








    • 3





      code == e.name is not going to work the way you meant it to, should be e.name.equals(code) instead

      – f_puras
      Nov 22 '16 at 14:40











    • @f_puras did you test the above code??

      – prashant thakre
      Nov 22 '16 at 15:40






    • 4





      Well, yes: SalaryHeadMasterEnum.getEnumByString("basic pay") does work, but relies on the JVM merging String literals. Stuff like SalaryHeadMasterEnum.getEnumByString(new StringBuilder("basic ").append("pay").toString()) does not, but should IMHO. Using String.equals() would make both work ;-)

      – f_puras
      Nov 22 '16 at 21:46






    • 1





      - for comparing strings with == which is bad and for using a variable 'name' with a different meaning than the 'name()' method

      – Hardcoded
      Apr 27 '17 at 15:09
















    • 3





      code == e.name is not going to work the way you meant it to, should be e.name.equals(code) instead

      – f_puras
      Nov 22 '16 at 14:40











    • @f_puras did you test the above code??

      – prashant thakre
      Nov 22 '16 at 15:40






    • 4





      Well, yes: SalaryHeadMasterEnum.getEnumByString("basic pay") does work, but relies on the JVM merging String literals. Stuff like SalaryHeadMasterEnum.getEnumByString(new StringBuilder("basic ").append("pay").toString()) does not, but should IMHO. Using String.equals() would make both work ;-)

      – f_puras
      Nov 22 '16 at 21:46






    • 1





      - for comparing strings with == which is bad and for using a variable 'name' with a different meaning than the 'name()' method

      – Hardcoded
      Apr 27 '17 at 15:09










    3




    3





    code == e.name is not going to work the way you meant it to, should be e.name.equals(code) instead

    – f_puras
    Nov 22 '16 at 14:40





    code == e.name is not going to work the way you meant it to, should be e.name.equals(code) instead

    – f_puras
    Nov 22 '16 at 14:40













    @f_puras did you test the above code??

    – prashant thakre
    Nov 22 '16 at 15:40





    @f_puras did you test the above code??

    – prashant thakre
    Nov 22 '16 at 15:40




    4




    4





    Well, yes: SalaryHeadMasterEnum.getEnumByString("basic pay") does work, but relies on the JVM merging String literals. Stuff like SalaryHeadMasterEnum.getEnumByString(new StringBuilder("basic ").append("pay").toString()) does not, but should IMHO. Using String.equals() would make both work ;-)

    – f_puras
    Nov 22 '16 at 21:46





    Well, yes: SalaryHeadMasterEnum.getEnumByString("basic pay") does work, but relies on the JVM merging String literals. Stuff like SalaryHeadMasterEnum.getEnumByString(new StringBuilder("basic ").append("pay").toString()) does not, but should IMHO. Using String.equals() would make both work ;-)

    – f_puras
    Nov 22 '16 at 21:46




    1




    1





    - for comparing strings with == which is bad and for using a variable 'name' with a different meaning than the 'name()' method

    – Hardcoded
    Apr 27 '17 at 15:09







    - for comparing strings with == which is bad and for using a variable 'name' with a different meaning than the 'name()' method

    – Hardcoded
    Apr 27 '17 at 15:09













    5














    Try, the following code..



        @Override
    public String toString() {
    return this.name();
    }





    share|improve this answer
























    • This should work well, but why call the function toString() instead of getName() (Or something similar) ?

      – Mayuso
      Oct 20 '16 at 11:15
















    5














    Try, the following code..



        @Override
    public String toString() {
    return this.name();
    }





    share|improve this answer
























    • This should work well, but why call the function toString() instead of getName() (Or something similar) ?

      – Mayuso
      Oct 20 '16 at 11:15














    5












    5








    5







    Try, the following code..



        @Override
    public String toString() {
    return this.name();
    }





    share|improve this answer













    Try, the following code..



        @Override
    public String toString() {
    return this.name();
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 20 '16 at 11:07









    JavaciJavaci

    6113




    6113













    • This should work well, but why call the function toString() instead of getName() (Or something similar) ?

      – Mayuso
      Oct 20 '16 at 11:15



















    • This should work well, but why call the function toString() instead of getName() (Or something similar) ?

      – Mayuso
      Oct 20 '16 at 11:15

















    This should work well, but why call the function toString() instead of getName() (Or something similar) ?

    – Mayuso
    Oct 20 '16 at 11:15





    This should work well, but why call the function toString() instead of getName() (Or something similar) ?

    – Mayuso
    Oct 20 '16 at 11:15











    1














    Here is the below code, it will return the Enum name from Enum value.



    public enum Test {

    PLUS("Plus One"), MINUS("MinusTwo"), TIMES("MultiplyByFour"), DIVIDE(
    "DivideByZero");
    private String operationName;

    private Test(final String operationName) {
    setOperationName(operationName);
    }

    public String getOperationName() {
    return operationName;
    }

    public void setOperationName(final String operationName) {
    this.operationName = operationName;
    }

    public static Test getOperationName(final String operationName) {

    for (Test oprname : Test.values()) {
    if (operationName.equals(oprname.toString())) {
    return oprname;
    }
    }
    return null;
    }

    @Override
    public String toString() {
    return operationName;
    }
    }

    public class Main {
    public static void main(String args) {

    Test test = Test.getOperationName("Plus One");
    switch (test) {
    case PLUS:
    System.out.println("Plus.....");
    break;
    case MINUS:
    System.out.println("Minus.....");
    break;

    default:
    System.out.println("Nothing..");
    break;
    }
    }
    }





    share|improve this answer






























      1














      Here is the below code, it will return the Enum name from Enum value.



      public enum Test {

      PLUS("Plus One"), MINUS("MinusTwo"), TIMES("MultiplyByFour"), DIVIDE(
      "DivideByZero");
      private String operationName;

      private Test(final String operationName) {
      setOperationName(operationName);
      }

      public String getOperationName() {
      return operationName;
      }

      public void setOperationName(final String operationName) {
      this.operationName = operationName;
      }

      public static Test getOperationName(final String operationName) {

      for (Test oprname : Test.values()) {
      if (operationName.equals(oprname.toString())) {
      return oprname;
      }
      }
      return null;
      }

      @Override
      public String toString() {
      return operationName;
      }
      }

      public class Main {
      public static void main(String args) {

      Test test = Test.getOperationName("Plus One");
      switch (test) {
      case PLUS:
      System.out.println("Plus.....");
      break;
      case MINUS:
      System.out.println("Minus.....");
      break;

      default:
      System.out.println("Nothing..");
      break;
      }
      }
      }





      share|improve this answer




























        1












        1








        1







        Here is the below code, it will return the Enum name from Enum value.



        public enum Test {

        PLUS("Plus One"), MINUS("MinusTwo"), TIMES("MultiplyByFour"), DIVIDE(
        "DivideByZero");
        private String operationName;

        private Test(final String operationName) {
        setOperationName(operationName);
        }

        public String getOperationName() {
        return operationName;
        }

        public void setOperationName(final String operationName) {
        this.operationName = operationName;
        }

        public static Test getOperationName(final String operationName) {

        for (Test oprname : Test.values()) {
        if (operationName.equals(oprname.toString())) {
        return oprname;
        }
        }
        return null;
        }

        @Override
        public String toString() {
        return operationName;
        }
        }

        public class Main {
        public static void main(String args) {

        Test test = Test.getOperationName("Plus One");
        switch (test) {
        case PLUS:
        System.out.println("Plus.....");
        break;
        case MINUS:
        System.out.println("Minus.....");
        break;

        default:
        System.out.println("Nothing..");
        break;
        }
        }
        }





        share|improve this answer















        Here is the below code, it will return the Enum name from Enum value.



        public enum Test {

        PLUS("Plus One"), MINUS("MinusTwo"), TIMES("MultiplyByFour"), DIVIDE(
        "DivideByZero");
        private String operationName;

        private Test(final String operationName) {
        setOperationName(operationName);
        }

        public String getOperationName() {
        return operationName;
        }

        public void setOperationName(final String operationName) {
        this.operationName = operationName;
        }

        public static Test getOperationName(final String operationName) {

        for (Test oprname : Test.values()) {
        if (operationName.equals(oprname.toString())) {
        return oprname;
        }
        }
        return null;
        }

        @Override
        public String toString() {
        return operationName;
        }
        }

        public class Main {
        public static void main(String args) {

        Test test = Test.getOperationName("Plus One");
        switch (test) {
        case PLUS:
        System.out.println("Plus.....");
        break;
        case MINUS:
        System.out.println("Minus.....");
        break;

        default:
        System.out.println("Nothing..");
        break;
        }
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 8 '17 at 8:28









        Fejs

        1,97111226




        1,97111226










        answered Sep 8 '17 at 7:28









        Do ItDo It

        53215




        53215






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3889248%2fjava-getting-the-enum-name-given-the-enum-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen