How to check if a sequence is empty in Kotlin? What's the simplest way?
2 Answers
If you mean literally a Sequence, use none() without arguments for "is empty" and any() for "is not empty".
For other collections, these method names work as well, but there's also
isNotEmpty(). Strangely, there is isEmpty, but only for arrays!
Comments
!sequence.iterator().hasNext()
2 Comments
Shreck Ye
I am not sure whether this is the simplest way. I don't know why there isn't an
isEmpty extension function for sequences.Martin Häusler
Careful with this method. If the sequence is an "only once" sequence, then any successive call to
iterator() will fail.