Difference Between Iterable and Collection in Java

1. Introduction

In Java, Iterable and Collection are two fundamental interfaces that are a part of the Java Collections Framework. The Iterable interface is the root interface for all the collection classes and provides the ability to iterate over a collection of objects. On the other hand, Collection extends Iterable and is the root interface of the collection hierarchy, representing a group of objects known as its elements.

2. Key Points

1. Iterable interface is primarily for implementing the "foreach" loop, and it defines only one method: iterator().

2. Collection interface includes all the functionality of Iterable, and adds methods to add, remove, and check the size of the collection, among others.

3. Every Collection is Iterable, but not all Iterables are Collections.

4. Iterable can be seen as a more general concept that allows an object to be the target of the "enhanced for loop" (for-each loop).

3. Differences

Iterable Collection
The root interface in Java for all collection classes that allows them to be iterated using the enhanced for-loop. A subinterface of Iterable represents a group of objects known as elements. It is more specific and comes with additional methods for data manipulation.
Defines a single method, iterator(), which returns an iterator over elements of type T. Extends the Iterable interface and includes methods for adding, removing, and examining elements, among others.
It’s a minimal interface that primarily enables for-each loop support over collections. Provides a richer, more extensive set of operations compared to Iterable, including bulk operations (like addAll, removeAll, retainAll), collection comparison (equals, hashCode), and element retrieval operations.
Any class implementing Iterable can be used in an enhanced for-loop. A more functional interface for storing and operating on groups of objects, with implementations including List, Set, and Queue.
Primarily focused on iteration rather than the management of collections. Focuses on the management (storage, retrieval, manipulation) of a collection of objects.

4. Example

// Importing necessary classes
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;

public class IterableVsCollection {
    public static void main(String[] args) {
        // Step 1: Create a Collection (ArrayList in this case)
        Collection<String> collection = new ArrayList<>();
        
        // Step 2: Add elements to the collection
        collection.add("Java");
        collection.add("Python");
        collection.add("C++");

        // Step 3: Iterate through the collection using Iterable interface
        Iterator<String> iterator = collection.iterator();
        while(iterator.hasNext()) {
            String language = iterator.next();
            System.out.println(language);
        }

        // Step 4: Use Collection methods directly
        System.out.println("Total languages: " + collection.size());
        System.out.println("Is Python in collection? " + collection.contains("Python"));
    }
}

Output:

Java
Python
C++
Total languages: 3
Is Python in collection? true

Explanation:

1. An ArrayList instance is created as a type of Collection.

2. Elements are added to the collection using Collection methods.

3. An iterator is obtained from the collection using the Iterable interface's iterator() method, and each element is printed.

4. Other Collection methods are used directly to display the size and check for an element's presence.

5. When to use?

- Use Iterable when you want to enable iteration over a collection of items using the for-each loop without the need for collection-specific functionality.

- Use Collection when you need a full-fledged collection that supports direct manipulation like adding, removing items, and querying its size.

Comments