I just wrote this but I'm not sure if this is good practice.  Can someone give me advice as to whether it's good or how I can do better or even it's bad code?The point is that I need a sub List-array-set of the enum.  The SubType.ELSE shall have another name in reality (I know that was a bad choice, but it's for pointing it out). public enum ContentType {    TITLE($$title$$,SubType.MAIL),    BODY($$body$$,SubType.MAIL),    MESSAGE($$message$$,SubType.MAIL),    EVENTS($$events$$,SubType.ELSE);    private final String replaceWord;    private final SubType subType;    private ContentType(String replaceWord, SubType subType ) {        this.replaceWord = replaceWord;        this.subType = subType;    }    public String getReplaceWord() {        return replaceWord;    }    private SubType getSubType() {        return subType;    }    public static List<ContentType> values(SubType subType) {        List<ContentType> subList = new ArrayList<ContentType>();        for (ContentType type : ContentType.values()) {            if (type.getSubType() == subType) {                subList.add(type);            }        }        return subList;    }}enum SubType {    MAIL,    ELSE;}
Enum with getting subset
java;enum
I suggest two changes to values(SubType):Return a Set instead of a List.Prepare all the possible results at class-loading time, so that values(SubType) could be accomplished by a simple lookup.  The code is slightly more complicated, but the runtime performance should be better.private static final Map<SubType, Set<ContentType>> BY_SUBTYPE;static {    BY_SUBTYPE = new HashMap<SubType, Set<ContentType>>();    for (SubType subType : SubType.values()) {        BY_SUBTYPE.put(subType, new TreeSet<ContentType>());    }    for (ContentType type : ContentType.values()) {        BY_SUBTYPE.get(type.getSubType()).add(type);    }    for (SubType subType : BY_SUBTYPE.keySet()) {        // Make Set<ContentType> values immutable        BY_SUBTYPE.put(subType, Collections.unmodifiableSet(BY_SUBTYPE.get(subType)));    }}public static Set<ContentType> values(SubType subType) {    // Returns an unmodifiable view of a SortedSet    return BY_SUBTYPE.get(subType);}