这很奇怪:A是一个集合而B是一组集合:
Set <String> A=new HashSet<String>();
Set <Set<String>> B=new HashSet<Set<String>>();
Run Code Online (Sandbox Code Playgroud)
我向他们添加了东西和输出
System.out.println(A)
Run Code Online (Sandbox Code Playgroud)
是:
[evacuated, leave, prepc_behind]
Run Code Online (Sandbox Code Playgroud)
和的输出
System.out.println(B)
Run Code Online (Sandbox Code Playgroud)
是:
[[leave, to, aux], [auxpass, were, forced], [leave, evacuated, prepc_behind]]
Run Code Online (Sandbox Code Playgroud)
可以看出,集合B的第三个元素等于设定A.所以假设
if(B.contains(A)){...}
Run Code Online (Sandbox Code Playgroud)
应该返回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");
}
Run Code Online (Sandbox Code Playgroud)
小智 -1
如果集合中的某个元素等于 other,则 Set.contains(other) 返回 true。
Set 重写了 equals() 和 hash()。如果两个集合具有相同的元素,Set.equals() 将返回 true。
因此,如果 A2 属于 B,并且 A2 与 A 具有相同的元素,则 B.contains(A) 将返回 true;
| 归档时间: |
|
| 查看次数: |
7811 次 |
| 最近记录: |