When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. The example provided below illustrates what should be avoided.

Noncompliant Code Example

List<String> objList = getData();

for (int i = 0; i < objList.size(); i++) {  // Noncompliant
    // execute code
}

Compliant Solution

List<String> objList = getData();

int size = objList.size();
for (int i = 0; i < size; i++) {
    // execute code
}