title: How to count the nested list elements size using Java 7?
body: <p>I have a Object which contains a list of another object which contains a list of another object and so on... suppose I want to get count of nested list elements(lets say last one), what should be best approach rather than using traditional for loop in java as I have done in below example - </p>
  
  <pre><code>public static void main(String[] args) {
  
      Statement statement = new Statement();
      statement.getInvAccount().add(new InvestmentAccount());
      statement.getInvAccount().get(0).getSecAccountStmt().add(new SecurityStatement());
      statement.getInvAccount().get(0).getSecAccountStmt().get(0).getTransactionStatement().add(new TransactionStatement());
      statement.getInvAccount().get(0).getSecAccountStmt().get(0).getTransactionStatement().add(new TransactionStatement());
      statement.getInvAccount().get(0).getSecAccountStmt().get(0).getTransactionStatement().add(new TransactionStatement());
  
      // method to count the number of TransactionStatement
      System.out.println("Size of TransactionStatement is : " + count(statement));
  
  }
  
  private static int count(Statement stmt) {
      int countOfTransStmt = 0;
      for (InvestmentAccount invAcc : stmt.getInvAccount()) {
          if (invAcc != null) {
              for (SecurityStatement secStmt : invAcc.getSecAccountStmt()) {
                  if (secStmt != null) {
                      countOfTransStmt = countOfTransStmt + secStmt.getTransactionStatement().size();
                  }
              }
          }
      }
      return countOfTransStmt;
  }
  </code></pre>