<

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll learn about the various ways in which we can get the size of an Iterable in Java.

2. Iterable and Iterator

Iterable is one of the main interfaces of the collection classes in Java.

The Collection interface extends Iterable and hence all child classes of Collection also implement Iterable.

Iterable has only one method that produces an Iterator:

public interface Iterable<T> {
    public Iterator<T> iterator();    
}

This Iterator can then be used to iterate over the elements in the Iterable.

3. Iterable Size using Core Java

3.1. for-each loop

All classes that implement Iterable are eligible for the for-each loop in Java.

This allows us to loop over the elements in the Iterable while incrementing a counter to get its size:

int counter = 0;
for (Object i : data) {
    counter++;
}
return counter;

3.2. Collection.size()

In most cases, the Iterable will be an instance of Collection, such as a List or a Set.

In such cases, we can check the type of the Iterable and call size() method on it to get the number of elements.

if (data instanceof Collection) {
    return ((Collection<?>) data).size();
}

The call to size() is usually much faster than iterating through the entire collection.

Here’s an example showing the combination of the above two solutions: 

public static int size(Iterable data) {

    if (data instanceof Collection) {
        return ((Collection<?>) data).size();
    }
    int counter = 0;
    for (Object i : data) {
        counter++;
    }
    return counter;
}

3.3. Stream.count()

If we’re using Java 8, we can create a Stream from the Iterable.

The stream object can then be used to get the count of elements in the Iterable.

return StreamSupport.stream(data.spliterator(), false).count();

4. Iterable Size using Third-Party Libraries

4.1. IterableUtils#size()

The Apache Commons Collections library has a nice IterableUtils class that provides static utility methods for Iterable instances.

Before we start, we need to import the latest dependencies from Maven Central:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

We can invoke the size() method of IterableUtils on an Iterable object to get its size.

return IterableUtils.size(data);

4.2. Iterables#size()

Similarly, the Google Guava library also provides a collection of static utility methods in its Iterables class to operate on Iterable instances.

Before we start, we need to import the latest dependencies from Maven Central:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>25.0</version>
</dependency>

Invoking the static size() method on the Iterables class gives us the number of elements.

return Iterables.size(data);

Under the hood, both IterableUtils and Iterables use the combination of approaches described in 3.1 and 3.2 to determine the size.

5. Conclusion

In this article, we looked at different ways of getting the size of an Iterable in Java.

The source code for this article and the relevant test cases are available over on GitHub.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS

Leave a Reply

avatar
  Subscribe  
Notify of