1

I have a very simple problem: I need to check if a large (150k) list of strings contains a certain string. Order does not matter, and I only need to check if the list contains a string. What is the most efficient data structure to use?

3
  • 3
    A list is a data structure. Are you instead asking what is the most efficient approach to using a list data structure to find a matching string? Commented Jun 19, 2015 at 17:52
  • If you have list already ---> Set<String> set = new HashSet<String>(list); Commented Jun 19, 2015 at 17:55
  • I would consider a Trie - Apache commons has a compressed Trie that performs well PatriciaTrie<E> Commented Jun 19, 2015 at 17:58

3 Answers 3

4

look at set (Hashset, enumset) and hash (HashMap,linkedhash...,idnetityhash..) based implementations, they have a speed complexity of O(1) for the contains() method.

this is a great link to use

Sign up to request clarification or add additional context in comments.

Comments

1

You want some structure that uses a hash function to insert, retrieve and delete elements. They usually have a theoretical O(1) complexity in those operations.

If all the strings are different, then you can use a HashSet. If you can have repeated elements, then you can use a HashMap that maps a String to an Integer that has how many of that elements you have.

Comments

0

you need to use a hashmap, it will give you O(1) complexity, just go ahead and store both the key and value pairs as hm.put(string,string); where hm is your hashmap.

Comments

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.