将这些数据存储在Java枚举中的最佳方法是什么?
<select>
<option></option>
<option>Recommend eDelivery</option>
<option>Require eDelivery</option>
<option>Require eDelivery unless justification provided</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我是java的新手并尝试过类似的东西
public enum Paperless {
"None" = null,
"Recommend eDelivery" = "Recommend eDelivery",
"Require eDelivery" = "Require eDelivery",
"Require eDelivery unless justification provided" = "Require eDelivery w/out justification"
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我正在考虑存储一个文本值的可能性,该值总结了用户在此网页上看到的选项.
我有一个枚举如下:
public enum ServerTask {
HOOK_BEFORE_ALL_TASKS("Execute"),
COPY_MASTER_AND_SNAPSHOT_TO_HISTORY("Copy master db"),
PROCESS_CHECKIN_QUEUE("Process Check-In Queue"),
...
}
Run Code Online (Sandbox Code Playgroud)
我还有一个字符串(比如string ="Execute"),我想根据它与之匹配的枚举中的字符串,将其作为ServerTask枚举的实例.有没有比在我想要匹配的字符串和枚举中的每个项目之间进行相等性检查更好的方法呢?因为我的枚举很大,所以看起来这会是很多if语句
我看到一些程序员在枚举结构中使用名为 fromValue 的函数。如果我们可以使用 valueOf,它的目的是什么?例如,我发现了这样的事情:
public static FooEnum fromValue(String v) {
return valueOf(v);
}
Run Code Online (Sandbox Code Playgroud) 请参阅下面的代码.这里基于String常量,我实例化了不同类型的组件类.现在至少有15种不同类型的String常量.因此,如果我遵循这种模式,将有15种不同的情况和那些很多if -else块.有没有更好的方法呢?我想通过尽可能少的代码更改来灵活地添加和删除案例.
public UIComponent initCellEditor(String editorType) {
UIComponent editControl = null;
if ("TbComboBoxCellType".equals(editorType)) {
editControl = new WebListEntryField();
editControl.setId("ComboBox");
} else if ("TbStringCellType".equals(editorType)) {
editControl = new WebInputEntryField();
editControl.setId("String");
} else if ("TbDateCellType".equals(editorType)) {
editControl = new WebDateEntryField();
editControl.setId("Date");
} else if ("TbDateTimeCellType".equals(editorType)) {
editControl = new WebDateTimeEntryField();
editControl.setId("DateTime");
} else {
//default editor is allways a text input
editControl = new WebInputEntryField();
editControl.setId("Input");
}
return editControl;
}
Run Code Online (Sandbox Code Playgroud)
PS:我们正在使用JDK 6.所以不能使用switch on String功能.
我有下面的枚举
enum Car {
lamborghini("900"),tata("2"),audi("50"),fiat("15"),honda("12");
private String price;
Car(String p) {
price = p;
}
String getPrice() {
return price;
}
}
public class Main {
public static void main(String args[]){
System.out.println("All car prices:");
System.out.println(Car.valueOf("lamborghini").getPrice());
for (Car c : Car.values())
System.out.println(c + " costs "
+ c.getPrice() + " thousand dollars.");
}
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我输入像"900",所以我想得到像lamborghini那样的enumConstructorName ,我该怎么做呢.
我需要从逗号分隔的String创建枚举列表.我在属性文件中有一些配置,主要是列表HttpStatus
喜欢:
some.config=TOO_MANY_REQUESTS,GATEWAY_TIMEOUT,BAD_GATEWAY
Run Code Online (Sandbox Code Playgroud)
此配置可以绑定到LIST,如下所示:
@Value("#{'${some.config}'.split(',')}")
private List<HttpStatus> statuses;
Run Code Online (Sandbox Code Playgroud)
现在可以用我的一行代码完成.我收到的字符串如下:
@Bean(name = "somebean")
public void do(@Value("${some.config:}") String statuses) throws IOException {
private List<HttpStatus> sList = StringUtils.isEmpty(statuses) ?
globalSeries : **Arrays.asList(statuses.split("\\s*,\\s*"));**
}
Run Code Online (Sandbox Code Playgroud)
Arrays.asList(series.split( "\ S*,\ S*")); 将创建一个字符串列表,现在我可以创建一个枚举列表,否则我需要迭代临时列表然后创建一个枚举列表.
我有这个代码,我想接受命令行args fx"12 EUR"进行转换:
public class Main {
enum Currency {EUR, USD, GBP,INVALID_CURRENCY;
static final float C_EUR_TO_DKK_RATE = (float) 7.44;
static final float C_USD_TO_DKK_RATE = (float) 5.11;
static final float C_GBP_TO_DKK_RATE = (float) 8.44;
static float result = 0;
static int amount = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Q1
if (args.length == 2) {
amount = Integer.parseInt(args[0]);
String currencyIn = args[1].toString();
Currency enumConversion = currencyIn; //**<---- HERE**
switch (enumConversion) …
Run Code Online (Sandbox Code Playgroud) G'day,勘误 ......我的计划如下所示.此更新旨在澄清并为深夜问题道歉.编译错误是由于文件中的其他地方的问题.
澄清:一个简单的Java 枚举,如下所示:
public enum ServiceSource
{
NONE,
URL,
FILE;
}
Run Code Online (Sandbox Code Playgroud)
想检查一下,isURL():
public boolean isURL(){
return (URL == this);
}
Run Code Online (Sandbox Code Playgroud)
这有效(并且编译)......毫无疑问 - 正确回答:dasblinkenlight和Elliott Frisch. 非常感谢你的时间.
也可以看看:
public enum EnumCountry implements EnumClass<Integer> {
Ethiopia(1),
Tanzania(2),
private Integer id;
EnumCountry(Integer value) {
this.id = value;
}
public Integer getId() {
return id;
}
@Nullable
public static EnumCountry fromId(Integer id) {
for (EnumCountry at : EnumCountry.values()) {
if (at.getId().equals(id)) {
return at;
}
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我有上面的代码.如何使用其枚举名称获取Enum Id.
嗨,我在尝试概括我为特定枚举编写的函数时遇到了麻烦:
public static enum InstrumentType {
SPOT {
public String toString() {
return "MKP";
}
},
VOLATILITY {
public String toString() {
return "VOL";
}
};
public static InstrumentType parseXML(String value) {
InstrumentType ret = InstrumentType.SPOT;
for(InstrumentType instrumentType : values()) {
if(instrumentType.toString().equalsIgnoreCase(value)) {
ret = instrumentType;
break;
}
}
return ret;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望在函数中添加一个新参数来表示任何枚举.我知道我应该使用模板但是我不能在函数代码中使用函数"values()".基本上我想要的是一个valueOf函数,它使用我定义的toString()值.
提前致谢.