Optimize list comparison method? I have below Java code to compare two list values with some condition:

 public boolean compareLists(List&lt;SortData&gt; prevList, List&lt;SortData&gt; modelList) {
        if (prevList != null &amp;&amp; modelList != null &amp;&amp; prevList.size() == modelList.size()) {
            boolean indicator = false;

            for (SortData modelListdata : modelList) {
                if (prevList.size() &gt; 1 &amp;&amp; modelList.size() &gt; 1 &amp;&amp; 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 &amp;&amp; modelListdata.getListBoxHeaderName() != null) {
                        if (prevList.size() == 1 &amp;&amp; modelList.size() == 1
                                &amp;&amp; !prevListdata.getListBoxHeaderName().equals(modelListdata.getListBoxHeaderName())) {
                            return true;
                        } else if (prevListdata.getListBoxHeaderName().equals(modelListdata.getListBoxHeaderName())
                                &amp;&amp; prevListdata.isSortAscending() != modelListdata.isSortAscending()) {
                            return true;
                        } else if (modelListdata.getListBoxHeaderName().equals(prevListdata.getListBoxHeaderName())
                                &amp;&amp; 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 &amp; 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 &gt; 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?
Some comments:

&amp;&amp; modelList.size() &gt; 1


this check is redundant, in fact

if (prevList.size() &gt; 1 &amp;&amp; modelList.size() &gt; 1 &amp;&amp; prevList.size() == modelList.size()) {


this whole check doesn't depend on the for variable, and can be hoisted outside the for.

The big if that starts at the first line can be inverted to reduce nesting.