I want to compare each elements of two java lists together, the lists could have null elements, but if I use .equals()
method I will get a NullPointerExeption
. How can I avoid this? How can I improve the code ?
if (l1!=null && l2!=null && l1.size()==l2.size()) {
for (int i = 0; i <l1.size(); i++) {
if (!l1.get(i).equals(l2.get(i))) {
return false;
}
}
}
return true;
how can I compare null elements in the lists with equal method?