Methods in Iterator interface
Method Description
boolean hasNext() Checks if the iterator has more
elements to traverse
E next() Moves the iterator to next element
and returns that element
void remove() Removes the last visited element from
underlying container class
Note:next() should have been called
before calling remove() else will throw
IllegalStateException
Methods in ListIterator interface
ListIterator extends Iterator interface
Method Description
boolean hasPrevious() Checks if the iterator has more
elements to traverse in reverse
direction
E previous() Moves the iterator to next element
and returns that element in reverse
direction
int nextIndex() Returns the index of the next element
in the iteration in forward direction
int previousIndex() Returns the index of the next element
in the iteration in reverse direction
void set(Element) Sets the last element visited (using
next or previous) .It replaces the
existing element
//Sorting ArrayList with same type
//Sorting can be done only if Collection is of same type
public class ArrayListSortDemo {
public static void main(String[] args) {
ArrayList lst=new ArrayList();
[Link](10);
[Link](25);
[Link](15);
[Link]((int)12.56);
[Link]("Before Sorting");
[Link](lst);
[Link](lst);
[Link]("After Sorting");
[Link](lst);
[Link]();
[Link]("Rajashekar");
[Link]("Suresh");
[Link]("Sudhakar");
[Link]("Vishnu");
[Link]("Aakash");
[Link]("Before Sorting");
[Link](lst);
[Link]("After Sorting");
[Link](lst);
[Link](lst);
[Link]("After shuffling");
[Link](lst);
Before Sorting
[10, 25, 15, 12]
After Sorting
[10, 12, 15, 25]
Before Sorting
After Sorting
[Aakash, Rajashekar, Sudhakar, Suresh, Vishnu]
After shuffling
[Rajashekar, Aakash, Vishnu, Suresh, Sudhakar]
LinkedList is combination of both list & queue
A LinkedList is ordered by index position, like ArrayList ,except that the
elements are doubly –linked to one [Link] and deleting is very
fast in LinkedList. But accessing elements is slow as we have to traverse.
When you want add or remove elements frequently we can use LinkedList
`Interview Question:
1)What is the difference b/w ArrayList & LinkedList & Vector
Note:LinkedList implements both interfaces List & DeQueue(It should act as both List as well as
Queue)
package [Link];
import [Link];
import [Link];
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList lst=new LinkedList();
[Link](100);
[Link]("Java");
[Link](12.45);
[Link](true);
[Link](lst);
[Link]("--print first item--");
[Link]([Link]());
[Link]([Link](0));
[Link]("--print last item--");
[Link]([Link]());
[Link]([Link]([Link]()-1));
[Link]("--print all items one by one--");
[Link]("Hello");
[Link](lst);
[Link]("Bangalore");
ListIterator litr=[Link]();
[Link]("Using ListIterator");
[Link]("Using hasNext()");
while([Link]())
{
[Link]([Link]());
}
[Link]("Using hasPrevious()");
while([Link]())
{
[Link]([Link]());
}
//You can also use simple for,enhanced for,
//toArray(),iterator() to traverse through items/objects
//descendingIterator
//peek,poll