Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!xlned.com!feeder1.xlned.com!Xl.tags.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 14 Sep 2011 14:36:42 -0500 Date: Wed, 14 Sep 2011 12:36:41 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: string case clauses References: <2oop671bkkrj4kg2cn59nq46ns1qg0n0qe@4ax.com> <16u177dof4qdro9a9pbps9ts7gce8gvuoq@4ax.com> In-Reply-To: <16u177dof4qdro9a9pbps9ts7gce8gvuoq@4ax.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 29 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.200.134 X-Trace: sv3-rQqR/7WPp9ASeUSFV8DPfr1UJiZzBEZk+6X/h4bXkHS6C1uqov31IIyKMWBR5uqWT9BSnCPt6LX05w7!8bLE5fHTnLQv/yZ0C4LjGP0SlEc0qyxt8ECmLKqETXqfL2v6u7Qbqma86fjeL+6PyDU7IMisPmQN!zn2XYFhWLqmvFCYonqz6FCbVfj1pnSKlxgXdasdPl7WNx9Y= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2769 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8033 On 9/14/2011 12:13 PM, Roedy Green wrote: > On Sun, 11 Sep 2011 20:05:20 -0700, Peter Duniho > wrote, quoted or indirectly quoted > someone who said : > >> Huh? How so? It's actually basically the same algorithm a full-on hash >> table would use. Specifically: hash value gets you to a very small >> subset of the possible values (very often just one, as long as the hash >> code algorithm is good), then linear search on collisions. > > Before case strings, there were two JVM instructions for implementing > an int switch, tableswitch and lookupswitch. The efficient tableswitch > is a jump table used when the switch values are reasonably dense e.g. > numbered 0..N. The less efficient lookupswitch is a binary search or > linear search used when the switch values all over the map. > > 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. Why do you assume it would be inefficient? If the number of cases is small, a linear search may well be faster than a HashMap lookup. If the number of cases is large, wouldn't it use a binary search? However, I don't like switch statements with a lot of cases anyway, because of unreadability and maintenance issues. Patricia