How does `Java` `List` method `size` work?

java, list

Solution

Size is defined as the number of elements in the list. The implementation does not specify how the size() member function operates (iterate over members, return stored count, etc), as List is an interface and not an implementation.

In general, most concrete List implementations will store their current count locally, making size O(1) and not O(n)

Problem

In Java, there is a `List` interface and `size()` method to compute the size of the `List`. - When I call `List.size()`, how does it count? - Is it counted linearly, or the count is determined and just the value is returned back when `size()`?

Original source

Related problems