0

I have been able to successfully load my ArrayList from the values of the enum class. I am not very familiar with using enums, and I was wondering if there was a way to handle this without typing add for each enum as I have shown.

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
  • Side issue: this Adjective("Placeholder [adjective] : To be updated..."), just doesn't look right as it looks like you're hard coding implementation details within a model, the enum. Likely better is the more simple ADJECTIVE("adjective"), ... or something similar. You would then create your "Placeholder [%s]: To be updated..." at some later place where you would display information to the user. Otherwise your code becomes rigid and hard to enhance, update and change later. Commented Feb 18, 2019 at 2:38

1 Answer 1

1

values() method return an array of values of enum. You can iterate over these values, get the speech value, and add them to the list.

Here is how you can do the above using the Stream API:

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

And following the Java naming convention, it should be posList and not POSList.

Sign up to request clarification or add additional context in comments.

4 Comments

Also regarding naming convention, the enum constants should be all upper-case.
The OP could get rid of speechValue and getSpeechValue(), using your code a la: List<String> posList = Arrays.stream(PartsOfSpeech.values()).map(pos -> String.format(format, pos.toString().toLowerCase())).collect(Collectors.toList());
oops, forgot the format String: String format = "\"Placeholder [%s] : To be updated...\"";
@HovercraftFullOfEels yup, it will make the enum cleaner. But I think the OP will be changing the placeholder to be updated part, so each of them have their unique value. If they don't do that, then it's not really a property of this enum and so the speechValue should be completely removed from it. It can be dynamically created (like you suggested using format) while inserting values in list/map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.