I have Book
and BookList
classes. BookList
is something like this:
public class BookList
{
private final List<
Implement the Iterable interface. That means you need to implement a method that returns an Iterator object that will iterate over the elements of a BookList
.
In this case, your iterator()
method could just return the result of calling bList.iterator()
. (That will cause for (Book b : somBookList)
to iterate over the Book
objects in the BookList.bList
... )
In other cases, you might need to write your own Iterator<T>
implementation class, complete with T next()
, boolean hasNext()
and remove()
methods. For instance, if you wanted to prevent external code from removing elements from the BookList
via your iterator, you might implement it like this:
public class BookList implements Iterable<Book> {
private final List<Book> bList = new ArrayList<Book>();
//...
@Override
public Iterator<Book> iterator() {
return new Iterator<Book> () {
private final Iterator<Book> iter = bList.iterator();
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public Book next() {
return iter.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException("no changes allowed");
}
};
}
}
You need to implement the Iterable interface, which means you need to implement the iterator()
method. In your case, this might look something like this:
public class BookList implements Iterable<Book> {
private final List<Book> bList = new ArrayList<Book>();
@Override
public Iterator<Book> iterator() {
return bList.iterator();
}
...
}
Here we can see the simple implementation of LinkedList with iterator and foreach syntax
class LinkedList implements Iterable<LinkedList.Node>{
private Node node;
public void add(Object data){
if(!Optional.ofNullable(node).isPresent()){
node = new Node();
node.setData(data);
}else{
Node node = new Node();
node.setData(data);
Node lastNode = getLastNode(this.node);
lastNode.setNext(node);
}
}
private Node getLastNode(Node node){
if(node.getNext()==null){
return node;
}else{
return getLastNode(node.getNext());
}
}
class Node{
private Object data;
private Node next;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public Iterator<Node> iterator() {
return new NodeIterator();
}
class NodeIterator implements Iterator<Node>{
private Node current;
public boolean hasNext() {
if(current == null){
current = node;
return Optional.ofNullable(current).isPresent();
}else{
current = current.next;
return Optional.ofNullable(current).isPresent();
}
}
public Node next() {
return current;
}
}
}
public class LinkedListImpl {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.add("data1");
linkedList.add("data2");
linkedList.add("data3");
for(LinkedList.Node node: linkedList){
System.out.println(node.getData());
}
}
}
To be more specific about how to "implement the Iterable
interface":
public class BookList implements Iterable<Book>
{
private final List<Book> bList = new ArrayList<Book>();
...
@Override
public Iterator<Book> iterator()
{
return bList.iterator();
}
}
For making a class iteratable you need to implement Iterable
Interface.
a) Declare the class as below
public class BookList implements Iterable<Book>
b) Override the iterator()
Method
Read More. Hope it will help you.