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");
}
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 .
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With