Question

I remember from way back at university using a switch with 'binary search' or 'binary switch'. Something like that, My google foo is broken today. Anyway it goes down like this: You define an array of possible options (Strings usually), some magic happens, and those options in the array become the cases in the switch happens. I do remember that they had to be in alphabetical order for this to work. Can you help me remember the magic? Even a name of what I should be looking at would be fantastic.

Was it helpful?

Solution

I think what you are looking for is an Enum.

From the link above...

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
    THURSDAY, FRIDAY, SATURDAY 
}

public class EnumTest {

    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
         switch (day) {
            case MONDAY:
                 System.out.println("Mondays are bad.");
                 break;

            case FRIDAY:
                 System.out.println("Fridays are better.");
                 break;

            case SATURDAY:
            case SUNDAY:
                 System.out.println("Weekends are best.");
                 break;

            default:
                 System.out.println("Midweek days are so-so.");
                 break;
        }
    }

    ....
}

OTHER TIPS

Did you mean gperf? Or possibly you were referring to the theory (hashing) in general?

http://www.gnu.org/software/gperf/

Normally, I wouldn't abuse a switch in such a way (even if I could). Try you might, you won't be able to get arrays to work in a switch statement because it only allows constant values in the case lines. Are you sure that you are not thinking of some pattern like below or an enumeration?

final int RED = 0;
final int YELLOW = 1;
final int BLUE = 2;
final int GREEN = 3;

String[] colors = new String[] { "red", "yellow", "blue", "green" };

switch (color) {
    case RED:
        System.out.println(colors[RED]);
        break;
    case YELLOW:
        System.out.println(colors[YELLOW]);
        break;
    ...the rest
}

Using an Enum without a switch looks like:

public enum Day {
    SUNDAY {
      public String tellItLikeItIs() {
        return "Weekends are best.";
      }
    },
    MONDAY {
      public String tellItLikeItIs() {
        return "Mondays are bad.";
      }
    }, 
    TUESDAY, 
    WEDNESDAY, 
    THURSDAY, 
    FRIDAY {
      public String tellItLikeItIs() {
        return "TGI Friday.";
      }
    }, 
    SATURDAY {
      public String tellItLikeItIs() {
        return "Weekends are best.";
      }
    }

    public String tellItLikeItIs() {
       return "Midweek days are so-so.";
    }
}

public class TodayIs{
    public static void main(String... args) {
         Day day = Day.valueOf(args[0].toUppercase());
         System.out.println(day.tellItLikeItIs());
    }
}
public enum Day {
    SUNDAY ("sundays are this"),
    MONDAY ("mondays are that"), 
    TUESDAY ("blah"), 
    WEDNESDAY ("blah"), 
    THURSDAY ("blah"), 
    FRIDAY ("blah"), 
    SATURDAY ("more blah");

    private final String tell;

    public Day(String tell){
       this.tell = tell;
    }
    public String tellItLikeItIs() {
       return this.tell;
    }
}

public class TodayIs{
    public static void main(String... args) {
         Day day = Day.valueOf(args[0].toUppercase());
         System.out.println(day.tellItLikeItIs());

I'm new to java enums, but I think this should work too.

If your choice is to use enum and your enum collection is of a big size use:

MAGIC:

If you are working in eclipse, it will do a "magic" for you just you need to write the following (taking into consideration the accepted answer's code):

switch (day) {
}

select the switch and press Ctrl + 1 and the magic works! (all the enum values will populate in the case sections of your switch block)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top