How to determine if a collection of collections contains another collection?

This is strange: A is a collection, and B is a collection of sets:

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

I added things to them, and the conclusion

 System.out.println(A) 

is an:

 [evacuated, leave, prepc_behind] 

and conclusion

 System.out.println(B) 

is an:

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

as you can see, the third element of the set B is equal to the set A. Thus, hypothetically

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

should return true, but apparently it is 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"); } 
+6
source share
2 answers

The main idea ( setA.contains(setB) == true ) works 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 jeopardize that you take more in your regular expression than you want. Try converting both the match from the regular expression and the test string to byte[] and test them against each other.

+2
source

Set.contains (other) returns true if the element belonging to the set is equal to another.

And Set has override equals () and hash (). Set.equals () will return true if both sets have the same elements.

So, if A2 belongs to B, and A2 has the same elements as A, B. contains (A) returns true;

-1
source

Source: https://habr.com/ru/post/903563/


All Articles