Search code examples
javaazureazure-storage

How to create a counter inside a for loop for Iterable in Java and get the value of the counter variable


Here's my current code snippet:

    Iterable<HashMap<String, EntityProperty>> results =
            cloudTable.execute(query, propertyResolver);

    if (results == null) {
        System.out.println("No files processed");
        exit_code = "exit_code=1";
    } else {
        for (HashMap<String, EntityProperty> entity : results) {
            // don't know how to start the loop here 
        }
    }

I have a query for retrieving a list of certain files in Microsoft Azure. Now I just need to show the number of files processed result.

I know the concept of what I should be doing, create a counter within the for loop, and then after the Iterations in that loop, whatever the value of that counter variable, it should also give me the count of files right? I just don't know how to start :( I've been reading so much about Iterable in Java but still can't get a grasp on how it would work.

Any inputs would be greatly appreciated


Solution

  • Like this?

    Iterable<HashMap<String, EntityProperty>> results =
            cloudTable.execute(query, propertyResolver);
    
    int counter;
    if (results == null) {
        System.out.println("No files processed");
        exit_code = "exit_code=1";
    } else {
        counter = 0;
        for (HashMap<String, EntityProperty> entity : results) {
            // don't know how to start the loop here 
            counter++;
        }
        //int size = results.size();
    }