How to check if iterable is empty in java?
An Iterable is an interface that represents a data structure that can be iterated over. It means each element in the data structure can be accessed one by one. It provides a method iterator(), which returns an Iterator object that is used to traverse a collection.
There are various methods to check if iterable is empty in java. This guide demonstrates these methods with easy-to-understand examples.
Scroll for More Useful Information and Relevant FAQs
- Check if iterable is empty Using Iterator.hasNext()
- Check if iterable is empty using Collection.isEmpty() (if applicable)
- Check if iterable is empty using Stream.count() (Java 8+)
- Check if iterable is empty using Apache Commons Collections IterableUtils.isEmpty() method
- Performance Considerations:
- Feedback: Your input is valuable to us. Please provide feedback on this article.
Check if iterable is empty Using Iterator.hasNext()
- Obtain an Iterator using the iterator() method.
- Invoke the
hasNext()
method on the Iterator to check if it has any more elements. - If hasNext() returns false means Iterable is empty.
Please refer to the code example to check if the iterable is empty.
Iterable<String> iterable = Arrays.asList();
if (!iterable.iterator().hasNext()) {
System.out.println("Iterable is empty");
} else {
System.out.println("Iterable is not empty");
}
Check if iterable is empty using Collection.isEmpty() (if applicable)
If the Iterable is also a Collection such as lists, tuples, sets, dictionaries, etc then directly call its isEmpty()
method to check empty. It’s the most efficient approach for Collections. The same method you can use to check if iterable is not empty in Java. Please check below example code
Collection<String> employee = new ArrayList<>();
if (employee.isEmpty()) {
System.out.println("Employee Array is empty");
} else {
System.out.println("Employee Array is not empty");
}
Check if iterable is empty using Stream.count() (Java 8+)
- Creates a Stream from a Spliterator.
- Convert Iterable to Spliterator using
Iterable. spliterator()
method. - Convert the Iterable to a Stream using
StreamSupport.stream(iterable.spliterator(), false)
from Supplier of Spliterator. - Call
count()
on the Stream to get the size. - If the count is 0 means Iterable is empty. Please refer to the code
Iterable<String> str = Arrays.asList("ABC", "XYZ");
long count = StreamSupport.stream(str.spliterator(), false).count();
if (count == 0) {
System.out.println("Iterable is empty");
}
Check if iterable is empty using Apache Commons Collections IterableUtils.isEmpty() method
In case you are using Apache Commons Collections then you can utilize IterableUtils supporting method isEmpty() to check the emptiness of iterable.
IterableUtils.isEmpty(iterable)
Performance Considerations:
- Use the Collection interface method isEmpty() to check if the collection is empty. It is generally the most efficient for Collections.
- Iterator.hasNext() is usually efficient for other Iterables. Generally, the hasNext() method is used in a loop to check if there are more values in the given collection.
- Try to avoid for-each or while loops in your case you only need to check emptiness because they iterate through all elements.
- Stream.count() can lead to decreased performance for large Iterables due to stream processing.