This method is defined by a superclass of List
which is common with possibly endless collections (like Stream
s, LazyList
s and Iterator
s).
For more details, I believe the documentation puts it best.
Here is the one for hasDefiniteSize
in version 2.13.1:
Tests whether this collection is known to have a finite size. All
strict collections are known to have finite size. For a non-strict
collection such as Stream, the predicate returns true if all elements
have been computed. It returns false if the stream is not yet
evaluated to the end. Non-empty Iterators usually return false even if
they were created from a collection with a known finite size.
Note: many collection methods will not work on collections of infinite
sizes. The typical failure mode is an infinite loop. These methods
always attempt a traversal without checking first that hasDefiniteSize
returns true. However, checking hasDefiniteSize can provide an
assurance that size is well-defined and non-termination is not a
concern.
Note that hasDefiniteSize
is deprecated with the following message:
(Since version 2.13.0) Check .knownSize instead of .hasDefiniteSize
for more actionable information (see scaladoc for details)
The documentation for knownSize
further states:
The number of elements in this collection, if it can be cheaply
computed, -1 otherwise. Cheaply usually means: Not requiring a
collection traversal.
List
is an implementation of a linked list, which is why List(1, 2, 3).hasDefiniteSize
returns true
(the collection is not boundless) but List(1, 2, 3).knownSize
returns -1
(computing the collection size requires traversing the whole list).