亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

嘗試從枚舉類創建字符串的 ArrayList

嘗試從枚舉類創建字符串的 ArrayList

呼如林 2022-06-23 16:14:09
我已經能夠從枚舉類的值中成功加載我的 ArrayList。我對使用枚舉不是很熟悉,我想知道是否有一種方法可以在不為每個枚舉鍵入 add 的情況下處理這個問題,正如我所展示的那樣。public enum PartsOfSpeech {    Adjective("Placeholder [adjective] : To be updated..."),    Adverb("Placeholder [adverb] : To be updated..."),    Conjunction("Placeholder [conjection] : To be updated..."),    Interjection("Placeholder [interjection] : To be updated..."),    Noun("Placeholder [noun] : To be updated..."),    Preposition("Placeholder [preposition] : To be updated..."),    Pronoun("Placeholder [pronoun] : To be updated..."),    Verb("Placeholder [verb] : To be updated...");    private String speechValue;    private PartsOfSpeech(String speechValue) {        this.speechValue= speechValue;    }    public String getSpeechValue() {        return speechValue;    }}public class Dictionary {    public static void main(String args[]) {        System.out.println("! Loading data...");        Map<String, List<String>> dictionaryMap = new HashMap<String, List<String>>();        List<String> POSList = new ArrayList<>();        POSList.add(PartsOfSpeech.Adjective.getSpeechValue());        POSList.add(PartsOfSpeech.Adverb.getSpeechValue());        POSList.add(PartsOfSpeech.Conjunction.getSpeechValue());        POSList.add(PartsOfSpeech.Interjection.getSpeechValue());        POSList.add(PartsOfSpeech.Noun.getSpeechValue());        POSList.add(PartsOfSpeech.Preposition.getSpeechValue());        POSList.add(PartsOfSpeech.Pronoun.getSpeechValue());        POSList.add(PartsOfSpeech.Verb.getSpeechValue());        dictionaryMap.put("distinct",POSList);
查看完整描述

1 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

values()方法返回枚舉值的數組。您可以遍歷這些值,獲取語音值,并將它們添加到列表中。

以下是使用 Stream API 執行上述操作的方法:

List<String> POSList = Arrays.stream(PartsOfSpeech.values())
        .map(PartsOfSpeech::getSpeechValue)
        .collect(Collectors.toList());

并且遵循 Java 命名約定,它應該是posList而不是POSList.


查看完整回答
反對 回復 2022-06-23
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號