我需要你的建议来改进下面的代码,因为它需要很多时间来执行method1和method2。当我执行RemoveFullyContains时,我正在调用method1和method2。我在这两种方法中都放了一个时间计数器,并且注意到执行这两种方法需要很多时间。可能有人可以给我一个指导来改善它。for循环代码需要提高性能
public static List<VG> RemoveFullyContains(List<VG> lTree) {
for (int x = lTree.size()-1; x >= 0; x--) {
VG vg1 = lTree.get(x);
for (int y = lTree.size()-1; y >= 0; y--) {
if (y != x) {
VG vg2 = lTree.get(y);
if (method1(vg2.getAndVar(), vg1.getAndVar())) {
if (method2(vg1.getNotVar(), vg2.getNotVar())) {
lTree.remove(x);
break;
}
}
}
}
}
return lTree;
}
private boolean method1(List<String> searchList, List<String> mainList) {
if (searchList == null || searchList.size() == 0) {
return true;
}
if (mainList == null || mainList.size() == 0) {
return false;
}
if (searchList.size() > mainList.size()) {
return false;
}
for (String item : searchList) {
if (!mainList.contains(item)) {
return false;
}
}
return true;
}
private boolean method2(List<String> list1, List<String> list2) {
if ((list1 == null || list1.size() == 0) && (list2 == null || list2.size() == 0)) {
return true;
}
if ((list1 == null || list1.size() == 0) || (list2 == null || list2.size() == 0)) {
return false;
}
if (list1.size() != list2.size()) {
return false;
}
for (String item : list1) {
if (!list2.contains(item)) {
return false;
}
}
return true;
}
VG is a class that has the following methods: hashcode, equal and clone
public class VG {
private List<String> andVar = new ArrayList();
private List<String> notVar = new ArrayList();
private List<VG> orVar = new ArrayList();
private VG parent;
....
}
有人可以帮我吗?对于上面的for循环,尝试用if(!mainList.contains(searcList))替换它,但是当我尝试显示计数器时间时,结果是相同的 –