Java iterable interface

The java iterable interface is defined in java.lang package. It is the topmost interface in the collection framework. Each sub-interface extends it directly or indirectly and each class implements it. The iterable interface has some method we will discuss them. It is defined as a generic type:

public interface Iterable<T> 

where T represents the type of elements returned by the iterator.

As we said the java Iterable interface is the super interface in the collection framework. It is the root interface for all the collection classes. The Collection interface extends the Iterable interface.

Java iterable interface

The iterable interface has three methods that one abstract method and two are default methods that were introduced in java 8. Each interface and class inherit/override these methods and provides implementation accordingly.

java iterator method

This method is used to iterate the collection in java. In the iterable interface it is an abstarct method because it has not any body here:

Iterator<T> iterator();

To use an iterator in your own class we need to provide it body. We can take an example from the ArrayList class, the ArrayList class provides the body to the iterator() method.

 public Iterator<E> iterator() 
{
        return new Itr();
}

Earlier, there were two ways to iterate the collection first one is for(;;) loop and by use of Iterator. The Iterator can get by invoking a collection object’s iterator() method.

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

public class ExampleOfForEach {

	public static void main(String[] args) {
		List<String> listOfNames = new ArrayList<>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("SITE");
		
		System.out.println("Using for loop");
		// First approch
		for (int i = 0; i < listOfNames.size(); i++) 
		{
		    System.out.println("Name = " +listOfNames.get(i));
		}
		
		System.out.println("Using for Iterator");
		// Second approch
		Iterator<String> iterator = listOfNames.iterator();
		while (iterator.hasNext()) {
		 
		    System.out.println("Name = "+iterator.next());
		}
	}

}

Output: Using for loop
Name = JAVA
Name = GOAL
Name = SITE
Using for Iterator
Name = JAVA
Name = GOAL
Name = SITE

forEach(Consumer action) method

This method was introduced in java 8. It accepts the parameter of consumer type. If you are a beginner then don’t focus it as of now. You have to read it Consumer interface in java and also need to see the details working forEach() method in java 8.

spliterator() method

It returns a object of Spliterator class. It is also used to iterate the collection. We will discuss it later.

 default Spliterator<T> spliterator() 
   {
        return Spliterators.spliteratorUnknownSize(iterator(), 0);
    }

1 thought on “Java iterable interface”

  1. I admire the useful facts you offer inside your articles. I will bookmark your weblog and have my kids check up here generally. I am fairly certain theyll learn lots of new things here than anyone else!

    Reply

Leave a Comment