How to detect if a Set of Sets contains another Set?

10,970

The basic idea (setA.contains(setB) == true) seems to work fine:

    Set<String>      set1 = new HashSet<String>();
    Set<Set<String>> set2 = new HashSet<Set<String>>();
    Set<String>      tmpSet;

    set1.add("one");
    set1.add("three");
    set1.add("two");

    tmpSet = new HashSet<String>();
    tmpSet.add("1");
    tmpSet.add("2");
    tmpSet.add("3");
    set2.add(tmpSet);

    tmpSet = new HashSet<String>();
    tmpSet.add("one");
    tmpSet.add("two");
    tmpSet.add("three");
    set2.add(tmpSet);

    System.out.println(set2.contains(set1)); // true

I would hazard a guess that you are capturing more in your regular expression then you would like. Try converting both the match from the regex and the test string to byte[] and checking them against each other.

Share:
10,970
Marcus
Author by

Marcus

Updated on August 11, 2022

Comments

  • Marcus
    Marcus over 1 year

    It is weird: A is a Set and B is a Set of Sets:

    Set <String> A=new HashSet<String>();
    Set <Set<String>> B=new HashSet<Set<String>>();
    

    I added things to them and the output of

    System.out.println(A)
    

    is:

    [evacuated, leave, prepc_behind]
    

    and the output of

    System.out.println(B)
    

    is:

    [[leave,  to, aux], [auxpass,  were, forced], [leave,  evacuated, prepc_behind]]
    

    as it can be seen, third element of set B equals to set A. So hypothetically

    if(B.contains(A)){...}
    

    should return true, but apparently it does not. What is the problem?

    More Details:

     Pattern pattern = Pattern.compile("(.*?)\\((.*?)\\-\\d+,(.*?)\\-\\d+\\).*");
        for (int i = 0; i < list.size(); i++) {
            Set <String> tp = new HashSet<String>();
            Matcher m = pattern.matcher(list.get(i).toString());
            if (m.find()) {
                tp.add(m.group(1).toLowerCase());
                tp.add(m.group(2).toLowerCase());
                tp.add(m.group(3).toLowerCase());
            }
            B.add(tp);
        }
        Set <String> A=new HashSet<String>();
        A.add("leave");
        A.add("evacuated");
        A.add("prepc_behind");
        System.out.println(A);
        if(B.contains(A)){
        System.out.println("B contains A");
    }