2

I need to create a Java structure to store a large number of String. Then I basically need to add new strings and also check if some string is already present... The order of the strings is not important.

I don't know many Java datatypes but the typical List, Set and Map, so... what would be the fastest datatype for this scenario? May it be a TreeSet or is there any other I'm missing?

MikO
  • 18,243
  • 12
  • 77
  • 109

1 Answers1

6

It depends on which kind of access you need.

  • sequential: LinkedList<String>
  • random: ArrayList<String>
  • check for presence: HashSet<String> (this is the one you are looking for according to your reqs)
  • check for presence and sorted traversal: TreeSet<String>
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Thanks @Jack, in fact, I don't need any kind of sorting, so I think I'll go for `HashSet`... – MikO May 13 '13 at 00:22
  • 1
    @MikO: HashSet is good if you have a lot of data. If your amount of data is small, then the cost of calculating the hash function adds some expense, but likely this is what you want. – Hovercraft Full Of Eels May 13 '13 at 00:23