Reputation: 4365
I'm making a java application that is going to be storing a bunch of random words (which can be added to or deleted from the application at any time). I want fast lookups to see whether a given word is in the dictionary or not. What would be the best java data structure to use for this? As of now, I was thinking about using a hashMap, and using the same word as both a value and the key for that value. Is this common practice? Using the same string for both the key and value in a (key,value) pair seems weird to me so I wanted to make sure that there wasn't some better idea that I was overlooking.
I was also thinking about alternatively using a treeMap to keep the words sorted, giving me an O(lgn) lookup time, but the hashMap should give an expected O(1) lookup time as I understand it, so I figured that would be better.
So basically I just want to make sure the hashMap idea with the strings doubling as both key and value in each (key,value) pair would be a good decision. Thanks.
Upvotes: 5
Views: 351
Reputation: 1961
If you're storing it as a collection of words in a dictionary, I'd suggest taking a look at Tries. They require less memory than a Set
and have quick lookup times of worst case O(string length)
.
Upvotes: 1
Reputation: 1588
My only concern would be memory, if you use the HashSet and if you have a very large collection of words... Then you will have to load the entire collection in the memory... If that's not a problem.... (And your collection must be very large for this to be a problem)... Then the HashSet should be fine... If you indeed have a very large collection of words, then you can try to use a tree, and only load in memory the parts that you are interested in.
Also keep in mind that insertion is fast, but not as fast as in a tree, remember that for this to work, Java is going to insert every element sorted. Again, nothing major, but if you add a lot of words at a time, you may consider using a tree...
Upvotes: 0
Reputation: 10040
Any class that is a Set
should help your purpose. However, Do note that Set
will not allow for duplicates. For that matter, even a Map
won't allow duplicate keys. I would suggest on using a an ArrayList
(assuming synchronization is not needed) if you need to add duplicate entries and treat them as separate.
Upvotes: 0
Reputation: 81134
I want fast lookups to see whether a given word is in the dictionary or not. What would be the best java data structure to use for this?
This is the textbook usecase of a Set
. You can use a HashSet
. The naive implementation for Set<T>
uses a corresponding Map<T, Object>
to simply mark whether the entry exists or not.
Upvotes: 9