很是怪异: A是一组和B是一套集:如何检测一组集合是否包含另一个集合?
Set <String> A=new HashSet<String>();
Set <Set<String>> B=new HashSet<Set<String>>();
我添加的东西,对他们的
System.out.println(A)
输出:
[evacuated, leave, prepc_behind]
和输出
System.out.println(B)
是:
[[leave, to, aux], [auxpass, were, forced], [leave, evacuated, prepc_behind]]
,因为它可以看出,集合B中的第三个元素等于设置A.所以,假设
if(B.contains(A)){...}
应返回true,但显然事实并非如此。问题是什么?
更多详细信息:
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");
}
你是如何添加元素的?因为'B.contains(A)'对我来说是正确的。 –
对我来说也按预期工作(即返回true)。 –
我有一个for循环,它使用:B.add(tp); tp是一个集合。 – Marcus