Basically I have about 1,000,000 strings, for each request I have to check if a String belongs to the list or not.
I’m worried about the performance, so what’s the best method? ArrayList? Hash?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your best bet is to use a
HashSetand check if a string exists in the set via thecontains()method. HashSets are built for fast access via the use of Object methodshashCode()andequals(). The Javadoc forHashSetstates:HashSet stores objects in hash buckets which is to say that the value returned by the
hashCodemethod will determine which bucket an object is stored in. This way, the amount of equality checks theHashSethas to perform via theequals()method is reduced to just the other Objects in the same hash bucket.To use HashSets and HashMaps effectively, you must conform to the
equalsandhashCodecontract outlined in the javadoc. In the case ofjava.lang.Stringthese methods have already been implemented to do this.