• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

best approach to compare a string value

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Regards,

Udayan
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Udayan Kumar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


Regards,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Example:

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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-ternary.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic