- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Iterable Interface in Java?
In simple words, the iterable interface is a common interface that allows us to iterate over a collection of objects. It was first introduced with the release of JDK 1.5 and made available in 'java.lang' package. The Java Collection Framework extends this interface, hence all the classes available in this collection framework by default implement the iterable interface. In other words, the classes of collection framework such as ArrayList, TreeSet, TreeMap and HashMap are iterable. This article aims to explain the iterable interface of Java along with its use case.
Iterable Interface in Java
The only use case exhibited by the Iterable interface is that it allows iterating over the elements of collection classes so that we can access those elements. One way to implement the Iterable interface is by overriding the 'iterator()' method and providing a concrete implementation of the Iterator interface. Here, the 'iterator()' method is an abstract method of Iterable interface. The other ways will be discussed later in this article. But, before moving on let's discuss a few important syntaxes.
General Syntax of Iterable Interface
public interface Iterable<TypeOfElement> { }
General Syntax of Iterator()
Iterator<TypeOfCollection> instanceOfIterator = nameOfCollection.iterator();
The Iterator interface provides the following methods
hasNext() − It returns true if there are more elements to iterate over.
next() − It returns the next element in the iteration if hasNext() returns true.
remove() − It removes the current element from the collection.
Example
The following example illustrates the use of iterator() method of Iterable Interface.
Approach
First, import the 'java.util' package so that we can access the ArrayList class.
Then, create an instance of ArrayList class and store a few elements in it using the built-in method 'add()'.
Next, create an iterator.
In the end, take a while loop that will iterate through ArrayList with the help of 'hasNext()' method and print the elements using 'next()' method.
import java.util.*; public class ArrayObj { public static void main(String[] args) { // Creating arraylist ArrayList<Integer> araylist = new ArrayList<>(); // Adding elements in arraylist araylist.add(1); araylist.add(2); araylist.add(1); araylist.add(0); araylist.add(9); araylist.add(6); System.out.println("Elements of the list: "); // creating an iterator Iterator<Integer> iterate = araylist.iterator(); // while loop to iterate through elements while (iterate.hasNext()) { // checking next element // printing the current element System.out.println(iterate.next()); } } }
Output
Elements of the list: 1 2 1 0 9 6
The other ways of iterating over iterable are as follows −
Using for-each loop
Using forEach()
Let's discuss them one by one.
Using for-each loop
If we use the enhanced for loop also known as for-each loop, then we can only access the elements of specified collection. It does not provide the feature of removing elements.
Example
In this example, we will use the for-each loop instead of the iterator() method like we did in the previous example.
import java.util.*; public class ArrayObj { public static void main(String[] args) { // Creating arraylist ArrayList<Integer> araylist = new ArrayList<>(); // Adding elements in arraylist araylist.add(1); araylist.add(2); araylist.add(1); araylist.add(0); araylist.add(9); araylist.add(6); System.out.println("Elements of the list: "); // for-each loop to iterate through elements for (Integer print : araylist) { // printing the elements System.out.println(print); } } }
Output
Elements of the list: 1 2 1 0 9 6
Using forEach()
It is the default method of Iterable Interface that uses a lambda expression to display the result. The lambda expression accepts a single input, performs the specified operation and returns nothing. It is one of the efficient ways to iterate over collection objects.
Example
In this example, we will use the forEach() in the place of iterator() method and for-each loop to get the desired result.
import java.util.*; public class ArrayObj { public static void main(String[] args) { // Creating arraylist ArrayList<Integer> araylist = new ArrayList<>(); // Adding elements in arraylist araylist.add(1); araylist.add(2); araylist.add(1); araylist.add(0); araylist.add(9); araylist.add(6); System.out.println("Elements of the list: "); // forEach to iterate through elements araylist.forEach(newArraylist -> // printing the elements System.out.println(newArraylist)); } }
Output
Elements of the list: 1 2 1 0 9 6
Conclusion
We started this article by defining the Iterable Interface and in the next section, we discussed it in detail. We have seen that there are a total of three ways of iterating through the iterables objects: iterator(), for-each loop and forEach(). Among these ways, iterator() is the most popular amongst developers.