Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

set

equals

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");
}
like image 643
Marcus Avatar asked Dec 12 '11 00:12

Marcus


People also ask

Can a set contains another set?

A set containing other sets is like a box containing other boxes. In this case, the set C has two elements, which are the two sets A and B . These elements of the set C are themselves sets; the set A has three elements, and the set B has four elements. Therefore it is true that A ∈ C , because A is an element of C .

How do you check if a set is a subset of another set in Java?

Example 1: Check subset of a Set using HashSet class numbers. containsAll(primeNumbers); Here, we have used the containsAll() method to check if primeNumbers is the subset of numbers .

How do you know if a set contains?

util. Set. contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element.

How do you compare sets in Java?

The equals() method of java. util. Set class is used to verify the equality of an Object with a Set and compare them. The method returns true if the size of both the sets are equal and both contain the same elements.


1 Answers

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.

like image 81
Assaf Avatar answered Sep 24 '22 11:09

Assaf