-4

I have a map which represents the key name (group id prefix) mapping between prefixName and the sequence name of each sequence defined in the database. For example, if the groupId is "GRP-MMM-PNL", I should get the corresponding sequence name "mmm_panel_group_id" of that group. My question is should I use Enum instead of defining this HashMap.

@AllArgsConstructor
public enum SequenceName {

    mmm_panel_group_id("GRP-MMM-PNL-"),
    mmm_service_group_id("GRP-MMM-SRV-"),
    
    cms_panel_group_id("GRP-CMS-PNL-"),
    cms_service_group_id("GRP-CMS-SRV-"),

    smm_panel_group_id("GRP-SMM-PNL-"),
    smm_service_group_id("GRP-SMM-SRV-");
    
    @Getter
    private String groupPrefix;
}

private static HashMap<String, String> getPrefixSequenceNameMap() {

        HashMap<String, String> prefixSequenceNameMap = new HashMap<>();
        prefixSequenceNameMap.put("GRP-MMM-PNL-", "mmm_panel_group_id");
        prefixSequenceNameMap.put("GRP-MMM-SRV-", "mmm_service_group_id");

        prefixSequenceNameMap.put("GRP-CMS-PNL-", "cms_panel_group_id");
        prefixSequenceNameMap.put("GRP-CMS-SRV-", "cms_service_group_id");

        prefixSequenceNameMap.put("GRP-SMM-PNL-", "smm_panel_group_id");
        prefixSequenceNameMap.put("GRP-SMM-SRV-", "smm_service_group_id");

        return prefixSequenceNameMap;
    }
2
  • please check stackoverflow.com/a/19600768 Commented Sep 15, 2020 at 4:26
  • Why are you asking? What makes you wonder specifically about it being an Enum instead of a Hashmap? Commented Sep 15, 2020 at 16:14

1 Answer 1

1

There is no "should". It depends on what you want.

  • If the values are known at compile-time and they do not change, an enum might be better. You can still use a Map though.
  • If the values are only known at run-time or they change while the program is running, use a Map.
Sign up to request clarification or add additional context in comments.

Comments

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.