Received: by 10.68.213.103 with SMTP id nr7mr2985978pbc.7.1353891226359; Sun, 25 Nov 2012 16:53:46 -0800 (PST) Received: by 10.50.34.167 with SMTP id a7mr5117278igj.5.1353891226293; Sun, 25 Nov 2012 16:53:46 -0800 (PST) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!kt20no8368053pbb.1!news-out.google.com!6ni6210pbd.1!nntp.google.com!kt20no8368047pbb.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Sun, 25 Nov 2012 16:53:45 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T NNTP-Posting-Host: 173.164.137.214 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <858d42ec-16f0-453f-971e-d3d7d0de3bdf@googlegroups.com> Subject: Re: SortedMap question? From: Lew Injection-Date: Mon, 26 Nov 2012 00:53:46 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com comp.lang.java.programmer:19967 Knute Johnson wrote: > That's what I thought, that the searching for a match would be very > quick once the data was in the TreeMap. The test code I wrote may not > have been any good. I created a map with Strings for the keys and > values. I could get about 2 million entries before I started running > into virtual memory issues slowing things down. I searched for > non-existent elements. Using both a HashMap and a TreeMap, the TreeMap > was significantly slower than the HashMap. I even tried a > ConcurrentHashMap and multiple threads to do the search to see if that > would speed things up. While that technique was better than TreeMap it > was still slower than a plain HashMap. > > So for the actual case that I am working on, I have a collection of > about 5000 elements and am using an Integer as the key. Data is rarely > changed but often accessed. There should never be a case where the > searched for key will not exist. What would you use, a HashMap or a > TreeMap? That would depend entirely on whether I needed to iterate the map in sorted order. There is little reason to prefer 'TreeMap' absent a sorting requirement except, as pointed out upthread, if you need guaranteed O(log n) performance for gets rather than the probabilistic promise of hashing. Such scenarios would require specialized knowledge of the data sets to be mapped. Kudos for your efforts to obtain objective performance evidence. -- Lew