containsKey (contains) implementation for Map (Set) to work with regex when key (contents) are Strin

C

chachra

Is there a containsKey (contains) implementation for Map (Set) to work
with regex when key (contents) are String's ?
 
E

Eric Sosman

chachra wrote On 01/09/07 16:45,:
Is there a containsKey (contains) implementation for Map (Set) to work
with regex when key (contents) are String's ?

If I understand you correctly, you are looking for
a method to report whether a Set (which might be the
keySet() of a Map) contains a String that is matched by
a given regular expression. I don't think such a thing
exists in the standard collection classes.

You could write one easily enough:

static boolean containsPattern(Set<String> set, Pattern pat)
{
for (String s : set) {
Matcher m = pat.matcher(s);
if (m.matches())
return true;
}
return false;
}


If the sets are large efficiency might be a concern,
and you might want to consider implementing a specialized
data structure. I'd suggest starting by asking whether you
really need a full-fledged regular expression, or merely a
few wild-cards and things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top