This post will discuss how to count the number of items in a List in Java.

1. Using List.size() method

The standard solution to find the number of elements in a Collection in Java is calling its size() method.

Download  Run Code

2. Using Stream API

With Java 8 Stream API, you can get a sequential stream over the list’s elements and call the count() method to get the count of elements in the stream.

Download  Run Code

3. Using Array.getLength() method

Another plausible way involves using Reflection. The idea is to convert the list into an array and call the Array.getLength() method to get its length. The toArray() method can be used to return an array containing all the elements in the list.

Download  Run Code

4. Using .length property

Alternatively, after converting the list into an array, you can directly access the .length property of the array, which returns the length of the array object.

Download  Run Code

That’s all about counting the number of items in a List in Java.