using enums or static string variable in java
got a questi开发者_如何学编程on around which one is better to use. Java5 Enums or static string.
I always get the data in form of Strings.
So for example,
private static final String LOAD_DRUGS = "load drugs";
or use Java5 enums
public enum LoadType
{
LOAD_DRUGS("load drugs");
}
In my code, I always receive "load drugs" kind of strings. I use if else statements to see what to do next based on it. But I am tending to use java5 enums and use switch case, but I always have to get the enum based of the string value I get.
So what are the pro's and con's of both ways??
Thanks!!
This answer is probably overkill. Maybe there's a badge for that. Anyway, it could be useful in a situation in which you have a lot of enumeration values and have to deal with a Strings as being the means by which another system sends information to you. That is exactly what I have (something north of 50), so I used this construct so that I could generate a mapping just once of the Strings reported by the db and the enums I used internally, and then not think about it after -- toString and fromString do all the work:
package com.stevej;
import com.google.common.collect.HashBiMap;
public enum TestEnum {
ALPHA("Alpha"), BETA("Beta"), GAMMA("Gamma");
private static HashBiMap<TestEnum, String> stringMapping = HashBiMap
.create(TestEnum.values().length);
private String stringValue = null;
TestEnum(String stringValue) {
this.stringValue = stringValue;
}
String getStringValue() {
return this.stringValue;
}
@Override
public String toString() {
return stringMapping.get(this);
}
public static TestEnum fromString(String string) {
return stringMapping.inverse().get(string);
}
static {
for (TestEnum e : TestEnum.values()) {
stringMapping.put(e, e.getStringValue());
}
}
}
Here's a quick test to show the data switching back and forth:
package com.stevej;
public class StackOverflowMain {
public static void main(String[] args) {
System.out.println(">> " + TestEnum.ALPHA);
System.out.println(">> " + TestEnum.BETA);
System.out.println(">> " + TestEnum.GAMMA);
TestEnum a = TestEnum.fromString("Alpha");
TestEnum b = TestEnum.fromString("Beta");
TestEnum c = TestEnum.fromString("Gamma");
System.out.println(">> " + a);
System.out.println(">> " + b);
System.out.println(">> " + c);
}
}
The output shows the use of the mixed case values instead of the uppercase, showing my strings are being used:
>> Alpha
>> Beta
>> Gamma
>> Alpha
>> Beta
>> Gamma
Note that I am using the Google Guava library so I can take advantage of the BiMap.
you can try a simple substitution for turn the string into an enum
switch(MyEnum.valueOf(text.replace(' ', '_')) {
case load_strings:
You can use toUpperCase() if you want it in upper case.
You should do what you think is the simplest and clearest.
Generally you should use Enums to store a set of values that are related in some way. They all should have a similar "is a" relationship. It should not be used to simply store a set of constant strings that are not related.
Of course, if you have a switch statement over a set of string values then that suggests that those string values are related and should be grouped as an Enum and use enum's mechanism to use a switch.
The enum type's valueOf method will allow you to convert from a String to the enum type if the passed string is equal to the name value. If this is not the case, you could implement your own valueOf that uses a stored string field rather than the name. You could store the fields in a private static Map for the conversion.
Because enum is typed.
Static String is not typed.
public enum Something{
CAT("cat");
private String animal;
private Something(String animal){
this.animal = animal;
}
public String getAnimal(){
return this.animal;
}
}
精彩评论