Initialize comparison datatable
Some problems that could be seen without actually understanding code:


Unhelpful and possibly misleading comments: // the selected year is not null
Repeating type in variable name : brandProfitAndLossCurrentYear
Using ad hoc code instead of using appropriate library methods: list.size() &lt;= 0 instead of list.isEmpty() and list.size() &gt; 0 instead of !list.isEmpty()
Long method doing more than one thing: initComparisonDatatable retrieve data from form, calculate something based using said data, modify form using calculated data
Feature Envy: Since initComparisonDatatable acts on brandActionForm and uses only data from brandActionForm it belongs to brandActionForm.
Repetitive code: body of the three loops are repeated with minor variations.


There are probable design issues, but those cannot be addressed properly without further clarification. For example new BrandProfitAndLoss().getAllRateComparisonRecords suggests BrandProfitAndLoss has some data access code in it, therefore should be moved elsewhere; it probably is a class method instead of an instance method; and since it is a class method it probably has dependency issues, I can smell an evil singleton from here.

How refactored code could look like, (since loops and conditional statements have been changed these will definitely break something, so do not try to use it in production without proper unit testing):

    private void initComparisonDatatable() {
        List&lt;BrandProfitAndLoss&gt; comparisonDatatable = listForDatatable(
                brandActionForm.getBrandProfitLossComparisonList(), 
                brandActionForm.getBrandProfitLossList(), 
                brandActionForm.getDateMode());

        brandActionForm.setBrandProfitLossComparisonListForDatatable(comparisonDatatable);
    }

    private static List&lt;BrandProfitAndLoss&gt; listForDatatable(
            List&lt;BrandProfitAndLoss&gt; brandProfitLossComparisonList,
            List&lt;BrandProfitAndLoss&gt; brandProfitLossList, Object dateMode) {
        List&lt;BrandProfitAndLoss&gt; comparisonDatatable = new ArrayList&lt;BrandProfitAndLoss&gt;();

        int prevYearSize = brandProfitLossComparisonList.size();
        int currentYearSize = brandProfitLossList.size();
        int size = Math.max(prevYearSize, currentYearSize);

        for (int i = 0; i &lt; size; i++) {
            BrandProfitAndLoss ofPreviousYear = (i &lt; prevYearSize) 
                    ? brandProfitLossComparisonList.get(i) 
                    : null;

            BrandProfitAndLoss ofCurrentYear = (i &lt; currentYearSize) 
                    ? brandProfitLossList.get(i) 
                    : null;

            if (ofPreviousYear == null) {
                ofPreviousYear = new BrandProfitAndLoss(
                        ofCurrentYear.getDate(), dateMode, Consts.CURRENT_YEAR);
            }

            if (ofCurrentYear == null) {
                ofCurrentYear = new BrandProfitAndLoss(
                        ofPreviousYear.getDate(), dateMode, Consts.CURRENT_YEAR);
            }

            comparisonDatatable.add(ofCurrentYear);
            comparisonDatatable.add(ofPreviousYear);
            comparisonDatatable
                    .add(new BrandProfitAndLoss().getAllRateComparisonRecords(
                            ofCurrentYear, ofPreviousYear));
        }

        return comparisonDatatable;
    }