• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching many times - HashMap best method?  RSS feed

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd probably put them in a List, sort them, and then use the Collections.binarySearch method.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!