9

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

2 Answers 2

18

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!

Sign up to request clarification or add additional context in comments.

Comments

2
!sequence.iterator().hasNext()

2 Comments

I am not sure whether this is the simplest way. I don't know why there isn't an isEmpty extension function for sequences.
Careful with this method. If the sequence is an "only once" sequence, then any successive call to iterator() will fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.