In Java, what is the fastest way to search for a word or substring in a growing list of strings?
For example, if I have a list of ten words, and I am searching that list every five minutes for an user-entered word, and that list grows by one word every minute, what would be the best data structure to store these words in?
What we are actually doing is this…upon retrieving a “keyword,” the program must search for phrases to respond with based on that keyword, but the list of phrases is constantly growing. It takes far too long to read the keyword, parse EVERY phrase, then pick a phrase. Our current algorithm is currently at n^3, which is inappropriate.
Are there data structures in Java, or sorting/searching algorithms that would help make this more efficient?
For huge, daunting search tasks, I always use Merge Sort. The fact that your list is growing every minute shouldn’t be a problem for the algorithm. You can combine this with another checker as you go to find the words you want. Actually, once you have the first list sorted, it may even make more sense to simply insert each element where it should be in the list when you receive it, rather than looking at the data only when you begin your search.
Keeping the list sorted this way will vastly improve your performance, assuming your rate of growth isn’t incredibly high.