In this article we will discuss how to iterate over a LinkedList Collection in Java and access objects.

To fetch the Iterator from a LinkedList object use following member function,

It will return ListIterator object that implements Iterator that provides functions to iterate over the LinkedList object. It manages an internal Cursor which is equivalent to head reference of Linked List data structure. This function will make the cursor to point to the first element if available.

ListIterator provides following functions,

It will return the next element in the list and updates the internal cursor to points to next element.

This function tells if there is any next element available or not.

It will return the prev element in the list and updates the internal cursor to points to prev element.

This function tells if there is any next element available or not.

There is also an overloaded version of listIterator() that will return the Iterator pointing to a specified index instead of start of LinkedList i.e.

Lets checkout an example. Here we will iterate over the LinkedList object in forward direction,

Output

So, this is how we can iterate over a LinkedList. In next article we will discuss how to iterate over a LinkedList in reverse direction.

 

Click Here to Subscribe for more Articles / Tutorials like this.