Getting the Size of an Iterable in Java – 在Java中获取Iterable的大小

最后修改: 2018年 5月 27日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

在这个快速教程中,我们将学习在Java中获得Iterable的大小的各种方法。

2. Iterable and Iterator

2.Iterable和Iterator

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

Iterable是Java中集合类的主要接口之一。

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

Collection接口扩展了Iterable,因此Collection的所有子类也实现了Iterable

Iterable has only one method that produces an Iterator:

Iterable只有一个方法可以产生一个Iterator

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

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

然后,这个Iterator可以用来遍历Iterable中的元素。

3. Iterable Size Using Core Java

3.使用Core Java的Iterable大小

3.1. for-each Loop

3.1.for-each 循环

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

所有实现Iterable的类都有资格用于Java中的for-each循环。

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

这允许我们在Iterable中的元素上循环,同时递增一个计数器以获得其大小。

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

3.2. Collection.size()

3.2.Collection.size()

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

在大多数情况下,Iterable将是Collection的一个实例,例如ListSet

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

在这种情况下,我们可以检查Iterable的类型,并对其调用size()方法来获得元素的数量。

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

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

size()的调用通常比遍历整个集合要快得多。

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()

3.3.Stream.count()

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

如果我们使用的是Java 8,我们可以从Iterable中创建一个Stream

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

然后可以使用流对象来获取Iterable中的元素数。

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

4. Iterable Size Using Third-Party Libraries

4.使用第三方库的可迭代大小

4.1. IterableUtils#size()

4.1.IterableUtils#size()

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

Apache Commons Collections库有一个很好的IterableUtils类,为Iterable实例提供静态实用方法。

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

在开始之前,我们需要从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.

我们可以在一个IterableUtils对象上调用size()方法来获得其大小。

return IterableUtils.size(data);

4.2. Iterables#size()

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.

类似地,Google Guava库也在其Iterables类中提供了一系列静态实用方法,以便对Iterable实例进行操作。

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

在开始之前,我们需要从Maven Central导入最新的依赖项。

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

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

Iterables类上调用静态size()方法,可以得到元素的数量。

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.

在引擎盖下,IterableUtilsIterables都使用3.1和3.2中描述的组合方法来确定大小。

5. Conclusion

5.总结

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

在这篇文章中,我们研究了在Java中获取Iterable的大小的不同方法。

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

本文的源代码和相关的测试案例可以在GitHub上找到