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?

1
  • 2
    Hashing is usually quite fast. Commented May 13, 2013 at 0:19

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Jack, in fact, I don't need any kind of sorting, so I think I'll go for HashSet...
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.