• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching many times - HashMap best method?

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

If I have 10000 strings, for example, and I want to read a file to see if each string I come across is in my 10000 strings, is HashMap the best way to store my list of 10000 as keys (assigning a null value to each) and use the containsKey() method the quickest method to see if they are in there or is there a quicker way of doing this? Note I am more interested in speed rather than memory usage here (assume I have infinite memory!).

Thanks.

Sam
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd probably put them in a List, sort them, and then use the Collections.binarySearch method.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also try a HashSet. It is backed by a HashMap but maybe it has some sort of optimisation to take care of the fact that the values are irrelevant.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashSet is what I'd try first, and see if that's fast enough. If it is, I wouldn't worry about whether it's perfect. There are specialised structures that might be faster, but you won't find them in the standard Java libraries (see Trie for example).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic