在Java中,我有两个ArrayLists:
A = [Ab, cd, df, FE, ...]
B = [ab, cde, de, fE, ...]
如果列表有点大,那么蛮力方法非常慢:
for(String a : A) {
for(String b : B) {
if(a.equalsIgnoreCase(b)) {
System.out.println("duplicate: " + a "->" + b);
}
}
}
什么是使其更快,但实施起来并不复杂的最佳方法?
答案 0 :(得分:2)
最简单的实现是使用集合,如下所示:
final String A[] = {"Ab", "cd", "df", "FE"};
final String B[] = {"ab", "cde", "de", "fE"};
final Set<String> set = new HashSet<>(A.length);
for (final String a : A)
set.add(a.toLowerCase());
for (final String b : B)
if (set.contains(b.toLowerCase()))
System.out.println("duplicate: " + b);