比较 ENUM 中的字符串

我想将启用或禁用的功能存储到数据库行中。当从他们那里收到一些字符串值时,我想将它与 ENUM 进行比较。


枚举:


public enum TerminalConfigurationFeatureBitString {

    Authorize("authorize", 0), // index 0 in bit string

    Authorize3d("authorize3d", 1), // index 1 in bit String

    Sale("sale", 2), // index 2 in bit String

    Sale3d("sale3d", 3), // index 3 in bit String

}


Map<TerminalConfigurationFeatureBitString, Boolean> featureMaps =

    config.initFromDatabaseValue(optsFromDatabase);


featureMaps.get(transaction.transactionType);

最好的方法是使用featureMaps.get(TerminalConfigurationFeatureBitString.Sale);


但我不知道传入的字符串会是什么。


现在我收到警告Unlikely argument type String for get(Object) on a Map<TerminalConfigurationFeatureBitString,Boolean>


有没有其他方法可以在不知道密钥的情况下对 ENUM 进行查询?


守着一只汪
浏览 137回答 3
3回答

慕后森

在这种情况下,我经常发现自己添加了一个静态方法getByX,该方法基于枚举的属性进行查找:public enum BitString {&nbsp; &nbsp; //...&nbsp; &nbsp; public static Optional<BitString> getByTransactionType(String transactionType)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Arrays.stream(values())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(x -> x.transactionType.equals(transactionType))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst();&nbsp; &nbsp; }}用法:enum TransactionStatus{&nbsp; &nbsp; ENABLED, NOT_ENABLED, NOT_SUPPORTED}TransactionStatus status = BitString.getBygetByTransactionType(transaction.transactionType)&nbsp; &nbsp; .map(bitString -> featureMaps.get(bitString))&nbsp; &nbsp; .map(enabled -> enabled ? TransactionStatus.ENABLED : TransactionStatus.NOT_ENABLED)&nbsp; &nbsp; .orElse(TransactionStatus.NOT_SUPPORTED);

犯罪嫌疑人X

与@Michael's answer类似,您可以static在您的内部生成一个查找映射enum,它将枚举事务类型映射到实际枚举:private static final Map<String, TerminalConfigurationFeatureBitString> TRANSACTION_TYPE_TO_ENUM =&nbsp;&nbsp; &nbsp;Arrays.stream(values()).collect(Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp;TerminalConfigurationFeatureBitString::getTransactionType,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Function.identity()&nbsp; &nbsp;);然后有一个查找方法,也在枚举内部:public static TerminalConfigurationFeatureBitString getByTransactionType(String transactionType) {&nbsp; &nbsp; TerminalConfigurationFeatureBitString bitString = TRANSACTION_TYPE_TO_ENUM.get(transactionType);&nbsp; &nbsp; if(bitString == null) throw new NoSuchElementException(transactionType);&nbsp; &nbsp; return bitString;}这在某种程度上比提到的答案更有效,因为它Map是在第一次加载时创建的enum(所以当它是第一次引用时)。因此迭代只发生一次。s也Map有一个相当快的查找时间,所以你可以说以这种方式获取枚举工作 O(1)(当忽略 O(n) 的初始计算时间时)

元芳怎么了

您可以enum使用额外的静态方法扩展您的方法,该方法将尝试转换给定String的enum项目:enum TerminalConfigurationFeatureBitString {&nbsp; &nbsp; Authorize("authorize", 0), // index 0 in bit string&nbsp; &nbsp; Authorize3d("authorize3d", 1), // index 1 in bit String&nbsp; &nbsp; Sale("sale", 2), // index 2 in bit String&nbsp; &nbsp; Sale3d("sale3d", 3); // index 3 in bit String&nbsp; &nbsp; private final String value;&nbsp; &nbsp; private final int index;&nbsp; &nbsp; TerminalConfigurationFeatureBitString(String value, int index) {&nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; &nbsp; &nbsp; this.index = index;&nbsp; &nbsp; }&nbsp; &nbsp; public String getValue() {&nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; }&nbsp; &nbsp; public int getIndex() {&nbsp; &nbsp; &nbsp; &nbsp; return index;&nbsp; &nbsp; }&nbsp; &nbsp; public static Optional<TerminalConfigurationFeatureBitString> fromValue(String value) {&nbsp; &nbsp; &nbsp; &nbsp; for (TerminalConfigurationFeatureBitString item : values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item.value.equals(value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Optional.of(item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return Optional.empty();&nbsp; &nbsp; }}如果未找到选项,请返回Optional.empty(). 如果特征不存在,则表示String表示不代表任何特征。用法:public void test() {&nbsp; &nbsp; EnumMap<TerminalConfigurationFeatureBitString, Boolean> featureMaps = new EnumMap<>(&nbsp; &nbsp; &nbsp; &nbsp; TerminalConfigurationFeatureBitString.class);&nbsp; &nbsp; Optional<TerminalConfigurationFeatureBitString> feature = TerminalConfigurationFeatureBitString.fromValue("authorize");&nbsp; &nbsp; if (!feature.isPresent()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Feature is not foudn!");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; Boolean authorize = featureMaps.get(feature.get());&nbsp; &nbsp; &nbsp; &nbsp; if (authorize != null && authorize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Feature is enabled!");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Feature is disabled!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java