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) {
System.out.println(spacecraft);
}
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)
{
spacecrafts.add(spacecraft);
}
public String get(int i)
{
return spacecrafts.get(i).toString();
}
public int length() {
return spacecrafts.size();
}
/*
* Iterabler
*/
class MyItr implements Iterator {
@Override
public boolean hasNext() {
if(nextCursor < spacecrafts.size())
{
return true;
}
return false;
}
@Override
public Object next() {
Object nextSpacecraft;
if(nextCursor == 0){
nextSpacecraft=spacecrafts.get(0);
nextCursor=nextCursor+1;
return nextSpacecraft;
}
nextSpacecraft=spacecrafts.get(nextCursor);
nextCursor=nextCursor+1;
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();
spacecrafts.add("Casini");
spacecrafts.add("Kepler");
spacecrafts.add("Galileo");
spacecrafts.add("Hubble");
Iterator it=spacecrafts.iterator();
while (it.hasNext()) {
String s=it.next().toString();
System.out.println(s);
}
spacecrafts.add("Spitzer");
spacecrafts.add("New Horizon");
spacecrafts.add("Voyager");
spacecrafts.add("Star Dust");
for (Object s : spacecrafts) {
System.out.println(s.toString());
}
}
}
Results
Casini
Kepler
Galileo
Hubble
Spitzer
New Horizon
Voyager
Star Dust