Methods in Iterator Interface

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

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();
lst.add(10);
lst.add(25);
lst.add(15);
lst.add((int)12.56);
System.out.println("Before Sorting");
System.out.println(lst);

Collections.sort(lst);
System.out.println("After Sorting");
System.out.println(lst);
lst.clear();

lst.add("Rajashekar");
lst.add("Suresh");
lst.add("Sudhakar");
lst.add("Vishnu");
lst.add("Aakash");
System.out.println("Before Sorting");
Collections.sort(lst);
System.out.println("After Sorting");
System.out.println(lst);
Collections.shuffle(lst);
System.out.println("After shuffling");
System.out.println(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 another.Inserting 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 com.qspiders.collectionsdemo;

import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListDemo {

public static void main(String[] args) {

LinkedList lst=new LinkedList();


lst.add(100);
lst.add("Java");
lst.add(12.45);
lst.add(true);
System.out.println(lst);
System.out.println("--print first item--");
System.out.println(lst.getFirst());
System.out.println(lst.get(0));
System.out.println("--print last item--");
System.out.println(lst.getLast());
System.out.println(lst.get(lst.size()-1));
System.out.println("--print all items one by one--");

lst.addFirst("Hello");
System.out.println(lst);
lst.addLast("Bangalore");

ListIterator litr=lst.listIterator();

System.out.println("Using ListIterator");
System.out.println("Using hasNext()");
while(litr.hasNext())
{
System.out.println(litr.next());
}
System.out.println("Using hasPrevious()");
while(litr.hasPrevious())
{
System.out.println(litr.previous());
}

//You can also use simple for,enhanced for,


//toArray(),iterator() to traverse through items/objects
//descendingIterator
//peek,poll

You might also like