QuizCure
Find Available Solution Here!

How to check if iterable is empty in java?

Deepak Dec 21, 2024

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.

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.

Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up