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

Discussion in 'Java' started by chachra, Jan 9, 2007.

  1. chachra

    chachra Guest

    Is there a containsKey (contains) implementation for Map (Set) to work
    with regex when key (contents) are String's ?
     
    chachra, Jan 9, 2007
    #1
    1. Advertising

  2. chachra

    Eric Sosman Guest

    Re: containsKey (contains) implementation for Map (Set) to work withregex when key (contents) are String's

    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.

    --
     
    Eric Sosman, Jan 9, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    673
  2. Jacek
    Replies:
    1
    Views:
    482
    Jacek
    Mar 7, 2004
  3. Marcelo Pinto
    Replies:
    4
    Views:
    441
  4. Patrick Guio
    Replies:
    6
    Views:
    3,247
    chris
    Oct 20, 2004
  5. Replies:
    3
    Views:
    809
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page