Too many for-loops and conditional operators Not please with this outcome, function works as is, but would love to simplify and prettify it. Use of one for-loop would be great and limit the use of the conditional operator.

private void checkForOverage(List&lt;GoogleUsageMapping&gt; records) {
    Long sumOfAllocatedSize = 0L;
    Long sumOfLogicalSize = 0L;
    for (GoogleUsageMapping record : records) {
        // initially all records are set to GoogleTrackingState.UNPROCESSED
        if (record.getUsage().getMeasuredType() == MeasuredType.ALLOCATED_SIZE) {
            sumOfAllocatedSize += record.getUsage().getQuantity().longValue();
        } else if (record.getUsage().getMeasuredType() == MeasuredType.LOGICAL_SIZE) {
            sumOfLogicalSize += record.getUsage().getQuantity().longValue();
        }
    }

    for (GoogleUsageMapping record : records) {
        if (sumOfAllocatedSize &gt;= sumOfLogicalSize &amp;&amp; record.getUsage().getMeasuredType() == MeasuredType.LOGICAL_SIZE) {
            record.setState(GoogleTrackingState.IGNORED_OVERAGE);
        } else if (sumOfLogicalSize &gt; sumOfAllocatedSize &amp;&amp; record.getUsage().getMeasuredType() == MeasuredType.ALLOCATED_SIZE) {
            record.setState(GoogleTrackingState.IGNORED_OVERAGE);
        }
    }
}
If you have not done so. I recommend picking up the book Refactoring: Improving the Design of Existing Code by Martin Fowler. It is an excellent book on exactly this topic.

One thing you can start with is to split the method by extracting each operation into a separate method. So that instead of doing everything at once you end up with a method per operation.

Doing so will let you examine what your code is actually doing and helps you see the patterns. Something like this:

private void checkForOverage(List&lt;GoogleUsageMapping&gt; records) {
    long allocatedSize = sumAllocated(records);
    long logicalSize = sumLogical(records);
    checkAllocated(records,allocatedSize, logicalSize);
    checkLogical(records,allocatedSize, logicalSize);
}

private long sumAllocated(List&lt;GoogleUsageMapping&gt; records) {
    long sumOfAllocatedSize = 0L;
    for (GoogleUsageMapping record : records) {
        if (record.getUsage().getMeasuredType() == MeasuredType.ALLOCATED_SIZE) {
            sumOfAllocatedSize += record.getUsage().getQuantity().longValue();
        }
    }
    return sumOfAllocatedSize;
}

private long sumLogical(List&lt;GoogleUsageMapping&gt; records) {
    long sumOfLogicalSize = 0L;
    for (GoogleUsageMapping record : records) {
        if (record.getUsage().getMeasuredType() == MeasuredType.LOGICAL_SIZE) {
            sumOfLogicalSize += record.getUsage().getQuantity().longValue();
        }
    }
    return sumOfLogicalSize;
}

private void checkAllocated(List&lt;GoogleUsageMapping&gt; records, long allocatedSize, long logicalSize) {
    for (GoogleUsageMapping record : records) {
        if (allocatedSize &gt;= logicalSize &amp;&amp; record.getUsage().getMeasuredType() == MeasuredType.LOGICAL_SIZE) {
            record.setState(GoogleTrackingState.IGNORED_OVERAGE);
        }
    }
}

private void checkLogical(List&lt;GoogleUsageMapping&gt; records, long allocatedSize, long logicalSize) {
    for (GoogleUsageMapping record : records) {
        if (logicalSize &gt; allocatedSize &amp;&amp; record.getUsage().getMeasuredType() == MeasuredType.ALLOCATED_SIZE) {
            record.setState(GoogleTrackingState.IGNORED_OVERAGE);
        }
    }
}


As you can see both pairs of methods are nearly identical. All you have to do to unify them is to extract the difference as parameter.

private void checkForOverage(List&lt;GoogleUsageMapping&gt; records) {
    long allocatedSize = sumByType(records, MeasuredType.ALLOCATED_SIZE);
    long logicalSize = sumByType(records, MeasuredType.LOGICAL_SIZE);
    check(records, allocatedSize &gt;= logicalSize, MeasuredType.ALLOCATED_SIZE);
    check(records, logicalSize &gt; allocatedSize, MeasuredType.LOGICAL_SIZE);
}

private long sumByType(List&lt;GoogleUsageMapping&gt; records, MeasuredType type) {
    long sumOfAllocatedSize = 0L;
    for (GoogleUsageMapping record : records) {
        if (record.getUsage().getMeasuredType() == type) {
            sumOfAllocatedSize += record.getUsage().getQuantity().longValue();
        }
    }
    return sumOfAllocatedSize;
}

private void check(List&lt;GoogleUsageMapping&gt; records, boolean condition, MeasuredType type) {
    for (GoogleUsageMapping record : records) {
        if (condition &amp;&amp; record.getUsage().getMeasuredType() == type) {
            record.setState(GoogleTrackingState.IGNORED_OVERAGE);
        }
    }
}