I have below Java code to compare two list values with some condition:
public boolean compareLists(List<SortData> prevList, List<SortData> modelList) {
if (prevList != null && modelList != null && prevList.size() == modelList.size()) {
boolean indicator = false;
for (SortData modelListdata : modelList) {
if (prevList.size() > 1 && modelList.size() > 1 && prevList.size() == modelList.size()) {
for (SortData prevListdata : prevList) {
if (modelListdata.getListBoxHeaderName().equals(prevListdata.getListBoxHeaderName())) {
indicator = false;
break;
}
else{
indicator = true;
}
}
}
for (SortData prevListdata : prevList) {
if (prevListdata.getListBoxHeaderName() != null && modelListdata.getListBoxHeaderName() != null) {
if (prevList.size() == 1 && modelList.size() == 1
&& !prevListdata.getListBoxHeaderName().equals(modelListdata.getListBoxHeaderName())) {
return true;
} else if (prevListdata.getListBoxHeaderName().equals(modelListdata.getListBoxHeaderName())
&& prevListdata.isSortAscending() != modelListdata.isSortAscending()) {
return true;
} else if (modelListdata.getListBoxHeaderName().equals(prevListdata.getListBoxHeaderName())
&& modelListdata.getPosition() != prevListdata.getPosition()) {
return true;
}
}
}
}
if (indicator) {
return true;
}
} else {
return true;
}
return false;
}
But I do not think this code is optimized.
The condition I am checking:
Both List not null
If both List size 1 & Name not null return true
If header Names equals but sortAcscending boolean variable not same return true.
If header Names equals but position a integer value not same return true
If both List size > 1 and bth list size equals then check same header name exist in both the List if not then return true.
I don't think the above code is optimized and I have to write two for
loops which look ugly. What can I do to optimize this code?