我正在比较两个字符串列表以查找可能的匹配项。例如:用于大型列表的正则表达式优化
public class Tester {
public static void main(String[] args) {
List<String> test = new ArrayList<String>();
List<String> test2 = new ArrayList<String>();
test.add("3H0875AAAA0012");
test.add("3H0875AABB0018");
test.add("3H0875AAAC0010");
test2.add("3H0875AA");
for(String s2: test2){
for (String s: test){
if (s.matches(".*" + s2 + ".*")){
System.out.println("Match");
}
}
}
}
}
基本上在test2
我想看看是否有test
包含test2
完全或部分的任何字符串每个字符串。上面的代码的输出应该是:
Match
Match
Match
然而,在我的真实情况是我在测试225K左右的字符串和TEST2 5K左右的字符串。这种比较需要很长的时间,并且希望看看是否有可能优化比较。测试2中的第一个1.5K项目需要大约10分钟的时间。所以完成比较至少需要30到40分钟。
在此先感谢
这是您忘记尝试最简单解决方案的日子之一 –