This discussion is archived
10 Replies Latest reply: May 2, 2012 5:00 AM by gimbal2 RSS

Map Vs Switch Case

782413 Newbie
Currently Being Moderated
I am having an Enum where I have two types of implementation to retreive the enum based on input parameter with getJobFrequency() :

1st implementation of getJobFrequency() with Switch case:


public enum JobFrequency {
     Daily(1),Weekly(2),Monthly(3), Yearly(4);
     
     private int value;

     private JobFrequency(intvalue) {
          this.value= value;
          }
     public int getValue(){
          return value;
     }
     public static JobFrequency getJobFrequency(int frequencyId){
          
          switch(frequencyId){
          case 1:
               return JobFrequency.Daily;
          case 2:
               return JobFrequency.Weekly;
          case 3:
               return JobFrequency.Monthly;
          case 4:
               return JobFrequency.Yearly;
          default:
               return null;
          }
     }


2nd implementation of getJobFrequency() with MAP:



public enum JobFrequency {
     Daily(1),Weekly(2),Monthly(3), Yearly(4);
     
     private int value;

private static final Map<Integer,JobFrequency > lookup
= new HashMap<Integer,JobFrequency >();

static {
for(JobFrequency s : EnumSet.allOf(JobFrequency .class))
lookup.put(s.getValue(), s);
}


     private JobFrequency(intvalue) {
          this.value= value;
          }
     public int getValue(){
          return value;
     }
     public static JobFrequency getJobFrequency(int frequencyId){
          
          return lookup.get(frequencyId);

          }
     }




Let me know which one is correct to use and why. Thanks a lot for the input
  • 1. Re: Map Vs Switch Case
    gimbal2 Guru
    Currently Being Moderated
    They are both correct because they both work.

    Any other type of answer for which is "better" depends on the person and the situation. If there are only a few values I'd use the switch statement. If there are lots of values or they can be dynamic, I'd use the map. But I would also dig a little deeper and see if that "frequencyId" cannot be removed and to just use the enum values everywhere.

    BTW: use \
     tags to format your code.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 2. Re: Map Vs Switch Case
    782413 Newbie
    Currently Being Moderated
    Hi,

    Thanks for the answer and the tip to use
    . My sincere  thanks.                                                                                                                                                                                        
  • 3. Re: Map Vs Switch Case
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    public static JobFrequency getJobFrequency(int frequencyId){
      try{
         return JobFrequency.values()[frequencyId -1]; // real programmers start with 0!!!
      } catch (ArrayIndexOutOfBoundsException ex) {
         throw new RuntimeException("invalid Job Frequency Id:" + frequencyId);
      }
    }
    bye
    TPD
  • 4. Re: Map Vs Switch Case
    EJP Guru
    Currently Being Moderated
    But why have frequencyID at all? Why not just use the enum values themselves?
  • 5. Re: Map Vs Switch Case
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    EJP wrote:
    But why have frequencyID at all? Why not just use the enum values themselves?
    I'm not the OP but I could imagin that the frequencyId comes from a configuration file or an input field...
    (OK in the latter case a ComboBox presenting only values defined in the enum would be better, but only the OP knows where the frequencyId comes from...)

    bye
    TPD
  • 6. Re: Map Vs Switch Case
    EJP Guru
    Currently Being Moderated
    The question remains. Why not use the name of the enum in all those places?
  • 7. Re: Map Vs Switch Case
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    EJP wrote:
    The question remains. Why not use the name of the enum in all those places?
    Then my question is: how do I get an enum value from a properties file (including a meningfull error message if the properties file contains an unknown value)?

    bye
    TPD
  • 8. Re: Map Vs Switch Case
    gimbal2 Guru
    Currently Being Moderated
    T.PD wrote:
    EJP wrote:
    The question remains. Why not use the name of the enum in all those places?
    Then my question is: how do I get an enum value from a properties file (including a meningfull error message if the properties file contains an unknown value)?

    bye
    TPD
    Use the valueOf function of the enum and catch the exception that may follow to know that the value is unknown.
  • 9. Re: Map Vs Switch Case
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    gimbal2 wrote:
    T.PD wrote:
    how do I get an enum value from a properties file (including a meningfull error message if the properties file contains an unknown value)?
    Use the valueOf function of the enum
    Your (unexpressed) suggestion is to use the same names in both, the input stream (what ever this may be) and the enum. Certainly a good advice unless the names need to be localized anyhow and/or the system delivering the numeric id's can't be changed.

    bye
    TPD
  • 10. Re: Map Vs Switch Case
    gimbal2 Guru
    Currently Being Moderated
    T.PD wrote:
    gimbal2 wrote:
    T.PD wrote:
    how do I get an enum value from a properties file (including a meningfull error message if the properties file contains an unknown value)?
    Use the valueOf function of the enum
    Your (unexpressed) suggestion is to use the same names in both, the input stream (what ever this may be) and the enum. Certainly a good advice unless the names need to be localized anyhow and/or the system delivering the numeric id's can't be changed.

    bye
    TPD
    Well no, I only gave a possible way to do what you specifically asked. How to read an enum value from a file or any arbitrary stream of characters? Put the String value into valueOf.

    If there is localization involved, I wouldn't be using enums.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points