Thursday, 5 September 2013

How to do a efficiently check if a string partially exists in a much larger set?

How to do a efficiently check if a string partially exists in a much
larger set?

Say I have a Set of Strings:
Set<String> things = new HashSet<String>();
things.add("coffee cup");
things.add("smartphone");
things.add("inkjet printer");
// :
// list could be quite large (100K or so, perhaps loaded from a database)
// :
Now I want to check if another string completely contains any of the
Strings in the above set. So:
"a coffee cup" - matches
"android smartphone" - matches
"inkjet printer for sale" - matches
"laser printer" - does not match
"printer" - does not match
The only way I can think of is iterating through the set (and break-ing if
found). Is there a more efficient and elegant way to do this?

No comments:

Post a Comment