第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

比較 ENUM 中的字符串

比較 ENUM 中的字符串

守著一只汪 2022-06-30 18:39:44
我想將啟用或禁用的功能存儲(chǔ)到數(shù)據(jù)庫(kù)行中。當(dāng)從他們那里收到一些字符串值時(shí),我想將它與 ENUM 進(jìn)行比較。枚舉: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);但我不知道傳入的字符串會(huì)是什么?,F(xiàn)在我收到警告Unlikely argument type String for get(Object) on a Map<TerminalConfigurationFeatureBitString,Boolean>有沒(méi)有其他方法可以在不知道密鑰的情況下對(duì) ENUM 進(jìn)行查詢?
查看完整描述

3 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

在這種情況下,我經(jīng)常發(fā)現(xiàn)自己添加了一個(gè)靜態(tài)方法getByX,該方法基于枚舉的屬性進(jìn)行查找:


public enum BitString {

    //...


    public static Optional<BitString> getByTransactionType(String transactionType)

    {

        return Arrays.stream(values())

            .filter(x -> x.transactionType.equals(transactionType))

            .findFirst();

    }

}

用法:


enum TransactionStatus

{

    ENABLED, NOT_ENABLED, NOT_SUPPORTED

}


TransactionStatus status = BitString.getBygetByTransactionType(transaction.transactionType)

    .map(bitString -> featureMaps.get(bitString))

    .map(enabled -> enabled ? TransactionStatus.ENABLED : TransactionStatus.NOT_ENABLED)

    .orElse(TransactionStatus.NOT_SUPPORTED);


查看完整回答
反對(duì) 回復(fù) 2022-06-30
?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

與@Michael's answer類似,您可以static在您的內(nèi)部生成一個(gè)查找映射enum,它將枚舉事務(wù)類型映射到實(shí)際枚舉:


private static final Map<String, TerminalConfigurationFeatureBitString> TRANSACTION_TYPE_TO_ENUM = 

   Arrays.stream(values()).collect(Collectors.toMap(

       TerminalConfigurationFeatureBitString::getTransactionType, 

       Function.identity()

   );

然后有一個(gè)查找方法,也在枚舉內(nèi)部:


public static TerminalConfigurationFeatureBitString getByTransactionType(String transactionType) {

    TerminalConfigurationFeatureBitString bitString = TRANSACTION_TYPE_TO_ENUM.get(transactionType);

    if(bitString == null) throw new NoSuchElementException(transactionType);

    return bitString;

}

這在某種程度上比提到的答案更有效,因?yàn)樗麺ap是在第一次加載時(shí)創(chuàng)建的enum(所以當(dāng)它是第一次引用時(shí))。因此迭代只發(fā)生一次。s也Map有一個(gè)相當(dāng)快的查找時(shí)間,所以你可以說(shuō)以這種方式獲取枚舉工作 O(1)(當(dāng)忽略 O(n) 的初始計(jì)算時(shí)間時(shí))


查看完整回答
反對(duì) 回復(fù) 2022-06-30
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以enum使用額外的靜態(tài)方法擴(kuò)展您的方法,該方法將嘗試轉(zhuǎn)換給定String的enum項(xiàng)目:


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


    private final String value;

    private final int index;


    TerminalConfigurationFeatureBitString(String value, int index) {

        this.value = value;

        this.index = index;

    }


    public String getValue() {

        return value;

    }


    public int getIndex() {

        return index;

    }


    public static Optional<TerminalConfigurationFeatureBitString> fromValue(String value) {

        for (TerminalConfigurationFeatureBitString item : values()) {

            if (item.value.equals(value)) {

                return Optional.of(item);

            }

        }


        return Optional.empty();

    }

}

如果未找到選項(xiàng),請(qǐng)返回Optional.empty(). 如果特征不存在,則表示String表示不代表任何特征。用法:


public void test() {

    EnumMap<TerminalConfigurationFeatureBitString, Boolean> featureMaps = new EnumMap<>(

        TerminalConfigurationFeatureBitString.class);


    Optional<TerminalConfigurationFeatureBitString> feature = TerminalConfigurationFeatureBitString.fromValue("authorize");

    if (!feature.isPresent()) {

        System.out.println("Feature is not foudn!");

    } else {

        Boolean authorize = featureMaps.get(feature.get());

        if (authorize != null && authorize) {

            System.out.println("Feature is enabled!");

        } else {

            System.out.println("Feature is disabled!");

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-06-30
  • 3 回答
  • 0 關(guān)注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)