Java:

  void myFunction() {
    int selectedItemsCount = countSelectedItems(cells);
  }

  private static int countSelectedItems(List<Cell> cells) {
    int count = 0;

    for (Cell cell : cells) {
      if (cell.isSelected()) {
        ++count;
      }
    }

    return count;
  }

Kotlin:

fun myFunction() {
  val selectedItemsCount = cells.count { it.selected }
}

Cathartic