Forums Register Login
best approach to compare a string value
Hi All,

Currently we have a scenario where in we need to match a String value with a huge list.
So the approach I thought of was to load all such String values in a String array & tehn in loop match the input value.

Would this be the right way for doing the same.

Do post your suggestions.


That would certainly work.

I have wondered myself, and sometimes use a hashmap instead. The theory being that the values is the map get hashed once when added (maybe at startup?), and then for every search, you're saving iterating over every single string.

I don't actually know if this REALLY is more efficient though, it just seems as though it should be. I'm not an expert on the internals.
It depends how many searches you're going to do.

If your program starts, loads the possible Strings, loads the one to search for, searches, then quits, then there isn't much you can do to speed things up (well, there's one thing, see below.) But if you're going to do multiple searches, then the short time it takes to put all the possibilities in a HashMap would definitely be worth it.

The one thing you can do in the one-search scenario: store the possible strings in sorted order in a file. Read them in, then use Arrays.binarySearch() to search the sorted array. This will be much faster than a linear search through the same array!
Thanks for all your response.

In my case I have a long list of String which gets populated in the array every time we invoke the method which requires the same.

So planning to make the String array a static instance.
With this I reckon the String array will be populated only once when the class in question is loaded.

Since its a pretty long list I was a bit sceptical of hard coding everything in the class. We are ruling out doing an I/O operation for the same as this we reckon would be an overhead though we can maintain the list in external file.

Aaron and Ernest are suggesting using a Map, like HashMap.

Note that a Map is a dictionary-like data structure, it maps keys to values. If you just have a bunch of strings and the only thing you want to do is find out if a string matches one of the strings, then a Set (like HashSet) is a more suitable data structure. No need to use a Map for this.


Searching in a Set is more efficient (faster) than in an array, especially if there are many strings. The Set stores the data in a certain ordered way so that it does not have to do a linear search, as you would have to do when all the strings are in an unordered array.

You could also do a more efficient search in an array with Arrays.binarySearch(...), but that requires that the elements in the array are sorted.
If the list of words doesn't change there are some very fast algorithms using trees. Prove that some simpler approach doesn't work before you go that far, tho.

Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Download Free Java APIs to Work with Office Files and PDF

This thread has been viewed 808 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 12, 2018 11:21:40.