Criteria Iterable Collection
Package java.lang java.util
Primary Purpose To provide a mechanism to iterate over a sequence of elements. Represents a group of objects and extends Iterable. It’s the root interface for most of the Java Collections Framework.
Main Methods iterator() add(), remove(), size(), isEmpty(), contains(), clear(), toArray(), and many more, in addition to iterator().
Sub-interfaces/Implementations None directly, but many classes and interfaces implement Iterable. Interfaces like Set, List, Queue, etc., and classes like ArrayList, HashSet, LinkedList, and more.
Usage Used when a class wants to allow iteration over its elements, even without being a full-fledged collection. Used when a class needs to represent and manage a group of elements with standard collection operations.
For-each Loop Any class implementing Iterable can be used with the enhanced for-each loop in Java. As Collection extends Iterable, any class implementing Collection can be used with the enhanced for-each loop.
Thread Safety Depends on the specific implementation. Standard implementations like ArrayList and HashSet are not thread-safe. Synchronization must be handled externally if concurrent modifications are expected.

Example: Difference Between Iterable and Collection in Java

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IterableVsCollectionDemo {

    public static void main(String[] args) {

        // Create an ArrayList which is a type of Collection
        Collection<String> collection = new ArrayList<>();
        collection.add("Java");
        collection.add("Python");
        collection.add("C++");

        // Demonstrating Collection methods
        System.out.println("Size of collection: " + collection.size());
        System.out.println("Is collection empty? " + collection.isEmpty());

        // As Collection extends Iterable, it can use the iterator() method
        Iterator<String> iterator = collection.iterator();
        System.out.print("Iterating over collection: ");
        while(iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }

        System.out.println();

        // Create a simple Iterable
        Iterable<String> iterable = new ArrayList<>(collection);

        // Iterating using the Iterable
        for (String lang : iterable) {
            System.out.print(lang + " ");
        }
    }
}

Output:

Size of collection: 3
Is collection empty? false
Iterating over collection: Java Python C++
Java Python C++

Explanation:

1. Iterable: It is an interface in Java that represents a group of objects which one can iterate over. The primary method it provides is iterator(), which returns an Iterator to loop over the elements.

2. Collection: It is also an interface and is a more advanced version of Iterable. Collection extends Iterable and represents a group of objects, with methods for adding, removing, and querying elements. It is the root interface in the collection hierarchy.

3. In the provided example:

– A Collection (ArrayList in this case) is created and populated with some programming languages.

– The size() and isEmpty() methods from the Collection interface are demonstrated.

– The iterator() method from the Iterable interface is used to iterate over the elements of the Collection.- A simple Iterable is created from the Collection and demonstrated.

4. When to use:

– Use Iterable when you only need to provide a way to iterate over elements.

– Use Collection when you need advanced operations like adding, removing, or querying elements.

In summary, Iterable is a base interface for any class that can be iterated over, while Collection provides more advanced capabilities and serves as the root for the Java collections framework.