Home » Core Java » util » Iterator » java.lang.Iterable Interface Example

About Prasad Saya

Prasad Saya
Prasad Saya is a software engineer with over ten years’ experience in application development, maintenance, testing and consulting on various platforms. He is a certified Java and Java EE developer. At present his interest is in developing Java applications. He also has experience working with databases and ERP applications.

java.lang.Iterable Interface Example

This article shows an example of Iterable interface. This is defined in java.lang package and was introduced with Java 5. The Iterable is defined as a generic type; Iterable<T>, where T type parameter represents the type of elements returned by the iterator.

An object that implements this interface allows it to be the target of the “foreach” statement. The for-each loop is used for iterating over arrays, collections etc. Collection classes and classes where iterations are useful implement this interface.

Before the iterable’s for-each loop was introduced, a way to iterate is to use the for(;;) loop or to use an Iterator; typically the Iterator could be acquired by invoking a collection object’s iterator() method. The iterator has been in Java since Java 1.2.

The Iterable interface provides a clean approach to coding the iteration functionality. An example shows iterating over a List collection with String object elements:

List<String> stringList = new ArrayList<>();
stringList.add("a");
stringList.add("b");
stringList.add("c");
for (String s : stringList) {

    System.out.println(s);
}

The above for-each code is much cleaner and elegant than the following two code snippets which have the same functionality. The following code use the for(;;) loop and the Iterator respectively.

for (int i = 0; i < stringList.size(); i++) {

    System.out.println(stringList [i]);
}
Iterator<String> iter = stringList.iterator();

while (iter.hasNext()) {

    System.out.println(iter.next());
}

Note that there is also a possibility of introducing a bug in the above two code snippets, because of the details.

1. Example

The following example class implements the Iterable interface. The class takes an input array of any type and iterates it in a for-each loop and in reverse order.

The Iterable interface has one method to override: Iterator<T> iterator().

The example has the iterable implementation MyIterable.java and a tester class MyIterableTester.java. The example code requires Java SE 5 or greater.

1.1. The code

MyIterable.java

import java.util.List;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Collections;

public class MyIterable<T> implements Iterable<T> {

    private List<T> list;

    public MyIterable(T [] t) {

        list = Arrays.asList(t);
        Collections.reverse(list);
    }

    @Override
    public Iterator<T> iterator() {

        return list.iterator();
    }
}

MyIterableTester.java

public class MyIterableTester {

    public static void main(String [] args) {

        Integer [] ints = {1, 2, 3};

        MyIterable<Integer> myList = new MyIterable<>(ints);

        for (Integer i : myList) {

            System.out.println(i);
        }
    }
}

1.2. The output

3
2
1

2. Download Java Source Code

This was an example of java.lang.Iterable Interface Example.

Download
You can download the full source code of this example here: IterableExample.zip
(No Ratings Yet)
2 Comments Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
B SShrey Sharma Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Shrey Sharma
Guest
Shrey Sharma

I had a Question…Even though we imported ‘java.util.iterator’ we didnt import ‘java.lang.iterable’ right, but still we were able to implement interface “Iterable”…does that supposedly mean that…
Interfaces :
Iterable and Iterator import each other in there library source code. Please explain since i saw java api regarding these, it shows that Iterable and Iterator interface dont implement each still the Iterable interface has a function ‘iterator()’…how?

B S
Guest
B S

all classes/interfaces in java.lang Pacakge are automatically available in java