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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Google Guava for getIfPresent() 按枚舉值進(jìn)行搜索

使用 Google Guava for getIfPresent() 按枚舉值進(jìn)行搜索

忽然笑 2022-06-30 17:38:03
public enum Dictionary {PLACEHOLDER1 ("To be updated...", "Placeholder", "adjective"),PLACEHOLDER2 ("To be updated...", "Placeholder", "adverb"),PLACEHOLDER3 ("To be updated...", "Placeholder", "conjunction");private String definition;private String name;private String partOfSpeech;private Dictionary (String definition, String name, String partOfSpeech) {    this.definition = definition;    this.name = name;    this.partOfSpeech = partOfSpeech;               }public String getName() {    return name;}public class DictionaryUser {    public static Dictionary getIfPresent(String name) {        return Enums.getIfPresent(Dictionary.class, name).orNull();    }    *public static Dictionary getIfPresent(String name) {        return Enums.getIfPresent(Dictionary.class, name.getName()).orNull();    }我最近剛剛遇到 getIfPresent() 基本上有一個全局靜態(tài)映射鍵在 Enum 類名上進(jìn)行查找。我遇到的問題是,我想使用我的 getter getName() 進(jìn)行查找,而不是使用枚舉名稱的名稱。在我提供的示例中,如果用戶輸入占位符,所有三個值都會顯示出來。這可以通過我的方法實現(xiàn)嗎?我在不起作用的方法旁邊放了一個 *。
查看完整描述

1 回答

?
慕的地8271018

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

由于您需要所有匹配的對象,但Enums.getIfPresent只會給您一個對象,因此您可以通過執(zhí)行以下操作輕松實現(xiàn)目標(biāo):


    public static Dictionary[] getIfPresent(String name)

    {

        List<Dictionary> response = new ArrayList<>(  );


        for(Dictionary d : Dictionary.values())

        {

            if( d.getName().equalsIgnoreCase( name ) )

            {

                response.add(d);

            }

        }

        return response.size() > 0 ?  response.toArray( new Dictionary[response.size()] ) : null;

    }


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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