Roedy Green wrote:
[...]
I understood the way string case labels worked posted earlier was to
use the hashCode as the switch variable, which would necessitate the
use of the inefficient lookupswitch plus a little kludge to deal with
hashCode collisions.
In contrast a String case lookup done manually with HashMap does not
involve any binary search or linear scan. It folds the hashCode to a
reasonable value and does a table lookup. [...]
I agree with Patricia's assessment. There is no proof that the current
implementation of an integral switch would be less efficient than a full
hash table implementation, and in fact for relatively few values (as a
switch really ought to be), it's likely to be more efficient. At the
very least, it's difficult to easily get a small hash table without also
having collisions.
I see no reason to be concerned whatsoever about the performance
implications of the choice of implementation for string case labels in
Java. If and when you have a program where that turns out to be a
bottleneck, then of course you can look at alternatives. I doubt you'll
run into that situation though. And if you do, it's quite likely that
you should have been using a hash table structure for other reasons anyway.
These points or similar were made fairly early in this thread by a couple of people.
A 'switch' by any other name is still a 'goto'.
But at least it's forward only.