Like Iterator, the Java ListIterator also a special reference over the collection of objects. In the past example on Java Iterator, we saw that it can only remove the collection items. But, the ListIterator can remove as well as add items to the collection while iteration is in progress. Unlike the Iterator, the ListIterator can iterate the collection in both forward and backward directions. So, the ListIterator is called a Bi-Directional Cursor.
In this example, we will use the ArrayList of Fruits and learn how ListIterator works. Let us start.
The Java Iterator is a reference over a collection object. It can be ArrayList, LinkedList anything which implements the basic Collection Interface. With the iterator we can get all the items in the collection one by one. We can iterate over the collection via a loop or via a for-each construct. But, using these, we cannot adjust the structure while the iteration is in-progress. Whereas with Java Iterator, we can remove items while the iteration is going on.
Java Iterator is a Forward-Only Cursor. This means one can iterate the collection items only in forward direction. Standard Java Iterator does not allow the reverse way of iteration. Also, note the Standard Java Iterator does not allow adding the items even though it allows removal. In this example, we will learn about the Iterator using an ArrayList collection object.
The Java Stack operates on ‘Last In First Out (LIFO)’ principle. In the past example, we used the LinkedList as Queue which operated on ‘First In First Out’. Let us say, the Hotel server is keeping the coffee cups on top of each one. When he serves the coffee to the tables, he will take out the cup from the top. This means, the person will take out cup which he placed last. This shows the LIFO principle. The same way Stack works in Java.
Java Collection does not provide Stack Interface. We can use the Deque interface which adds element in the front (or we can say Top) and removes element from the front using special methods of stack. In this example, we are going to use our Linked List of Products as stack.
The Java Deque (Double-Ended-Queue) interface extends the Queue interface and Java’s LinkedList implements Deque interface. In the past coding examples, we used the LinkedList as queue via the Java Queue Interface. In this example, we will use our LinkedList using the Deque Interface. Unlike queue, we can add and remove items from both ends of the LinkedList via Java Deque Interface. The Deque is also called as ‘Deck’.
The Queue operates on ‘First In First Out (FIFO)’ principle. This means an element added first to the Queue removed first from the queue. For example, in a railway ticket counter the person who enters the queue first will be served first and removed from the queue. Each new person coming to buy the ticket will stand in the tail end of the queue. The same Queue will work in Java. In simple words, elements are added to the tail end of the Queue and removed from the head of the Queue.
In this example, we will see how to use Java Linked as a Queue.