Java Iterable Interface

| Page Views: 59

Iterable is an interface whose implementers can be used with an enhance for loop.

Basically the implementer will be usable with a foreach loop. e.g

    for (String spacecraft : spacecrafts) {

Characteristics of Iterable Interface

Here are the characteristics of this interface with regards to its definitions:

First it's an interface:

public interface Iterable<T> {}

That belongs to java.lang package.

package java.lang;

Functionality of Iterable

No. Functionality
1. As an interface Iterable contains one abstract method called iterator(). This method when implemented will return a generic Iterator<T> for elements of the invoker.
2. Iterable allows us create a class that can be looped over via the foreach loop.

Using an Iterable

It's an interface so just implement:

class Spacecrafts<T> implements Iterable<T> {..}

Then implement the iterator() method:

    public Iterator<T> iterator() {
        return null;

Full Example

Here's a complete Iterator example with a foreach loop:

1. Spacecrafts class

No. Role
1. This is the to be iterator. It represents a collection of spacecraft strings though it's not a collection. Instead it maintains a private arraylist to be used for holding its data.
2. Implements iterable interface and overrides the iterator<T>() method.
3. Defines a method to allow for adding of data to be looped via the foreach loop.
class Spacecrafts implements Iterable {

    int nextCursor = 0;
    private ArrayList spacecrafts=new ArrayList<>();
    public Iterator iterator() {
        return new MyItr();

    public void add(Object spacecraft)
    public String get(int i)
        return spacecrafts.get(i).toString();
    public int length() {
        return spacecrafts.size();

     * Iterabler
    class MyItr implements Iterator {

        public boolean hasNext() {
            if(nextCursor < spacecrafts.size())
                return true;
            return false;
        public Object next() {
            Object nextSpacecraft;
            if(nextCursor == 0){
                return nextSpacecraft;
            return nextSpacecraft;


1. Iterabler class

Here are the roles of this class: No. Role
1. This is our main class. It contains our main method that will be executed when we run the project.
2. We instantiate our Spacecrafts class and add data to it.
3. Then loop through that data via foreach loop displaying in the console.

Just add the following code above the Spacecrafts class in the same file:

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

public class Iterable_Iterator  {

    public static void main(String[] args) {
        Spacecrafts spacecrafts=new Spacecrafts();


        Iterator it=spacecrafts.iterator();
        while (it.hasNext()) {

        spacecrafts.add("New Horizon");
        spacecrafts.add("Star Dust");

        for (Object s : spacecrafts) {



New Horizon
Star Dust

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post