Shreck Ye
Shreck Ye

Reputation: 1859

How to check if a sequence is empty in Kotlin

How to check if a sequence is empty in Kotlin? What's the simplest way?

Upvotes: 8

Views: 3913

Answers (2)

Alexey Romanov
Alexey Romanov

Reputation: 170745

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!

Upvotes: 17

Shreck Ye
Shreck Ye

Reputation: 1859

!sequence.iterator().hasNext()

Upvotes: 1

Related Questions