Converting Enum into String and parsing String to Enum in Java is becoming a common task with the growing use of Enum. Enum is very versatile in Java and preferred the choice to represent bounded data and since is almost used everywhere to carry literal value it's important to know how to convert Enum to String in Java. In this article, we will see both first converting Strings to Enum in Java and then Change an Enum to String in Java with Example. I thought about this Enum tutorial when I wrote 10 Examples of Enum in Java. I missed String to Enum conversion and one of the readers pointed out that. So here we have now.
Enum to String to Enum in Java
This article is in continuation of other conversion-related posts e.g. how to convert Date to String in Java and How to Convert String to Integer in Java. As these are common needs and having the best way to do things in mind saves a lot of time while coding.
Convert Enum to String in Java Example
Enum classes by default provide the valueOf (String value) method which takes a String parameter and converts it into an enum. String name should match with the text used to declare Enum in the Java file. Here is a complete code example of String to Enum in Java
Code Example String to Enum:
/**
* Java Program to parse String to Enum in Java with examples.
*/
public class EnumTest {
private enum LOAN {
HOME_LOAN {
@Override
public String toString() {
return "Always look for cheaper Home loan";
}
},
AUTO_LOAN {
@Override
public String toString() {
return "Cheaper Auto Loan is better";
}
},
PEROSNAL_LOAN{
@Override
public String toString() {
return "Personal loan is not cheaper any more";
}
}
}
public static void main(String[] args) {
// Exmaple of Converting String to Enum in Java
LOAN homeLoan = LOAN.valueOf("HOME_LOAN");
System.out.println(homeLoan);
LOAN autoLoan = LOAN.valueOf("AUTO_LOAN");
System.out.println(autoLoan);
LOAN personalLoan = LOAN.valueOf("PEROSNAL_LOAN");
System.out.println(personalLoan);
}
}
Output:
Always look for cheaper Home loan
Cheaper Auto Loan is better
Personal loan is not cheaper anymore
Convert Enum to String in Java Example
Now let's do the opposite convert an Enum into String in Java, there are multiple ways to do it one way is to return the exact same String used to declare Enum from toString() method of Enum, otherwise if you are using toString() method for another purpose then you can use default static name() method to convert an Enum into String.Java by default adds the name() method into every Enum and it returns exactly the same text which is used to declare enum in a Java file.
Code Example Enum to String
public static void main(String[] args) {
// Java example to convert Enum to String in Java
String homeLoan = LOAN.HOME_LOAN.name();
System.out.println(homeLoan);
String autoLoan = LOAN.AUTO_LOAN.name();
System.out.println(autoLoan);
String personalLoan = LOAN.PERSONAL_LOAN.name();
System.out.println(personalLoan);
}
Output:
HOME_LOAN
AUTO_LOAN
PERSONAL_LOAN
That’s all on How to parse String to Enum in Java and convert Enum to a String object. This tip will help you to quickly convert your data between the two most versatile types Enum and String in Java. If you know any other way to change String to Enum in java then please let us know.
Related post:
Thanks for you comment Kiran, Good to know that you like this Enum to String conversion tutorial.
ReplyDeletehi, by default when we convert Enum to String its return face value of Enum i.e. how it has written, is there any way we can get customized String values from Enum, I am thinking of method like toString, does Enum provides toString() in Java ?
ReplyDeleteI don't like the above example. In my opinion making every enum value an anonymous class makes it too complex. Hardcodec strings are not good too and, finally, enum converted to string and from string back to enum won't be the same. I've had in my job a situation when I had to make an enum from strings incoming from the remote server an then I started to use a pattern like this:
ReplyDeletepublic enum Example
{
UNKNOWN(""),
SECURE("https://"),
INSECURE("http://");
private final String id;
private Example(String aId)
{
id = aId;
}
@Override
public String toString()
{
return id;
}
public Example fromString(String aId)
{
Example result = UNKNOWN;
for (Example candidate: values())
{
if (candidate.toString().equals(aId))
{
result = candidate;
break;
}
}
return result;
}
}
If you try to convert a String to enum for a String that does not match exactly to the name of a value of your enum it will throw an exception. Wrapping your conversion with a try catch can be tedious, especially when it happens frequently in your application. If your enum has only a few values, iterating through them for a match can be faster than using a try catch and returns a null for missing value instead of throwing an exception.
ReplyDeletepublic enum LOAN {
AUTO, FIRE, HOME;
public static LOAN get(final String s) {
for (final LOAN value : LOAN.values()) {
if (value.toString().equalsIgnoreCase(s)) {return value;}
}
return null;
}
public static void main(final String[] args) {
System.out.println(LOAN.get("FARM"));
}
}