Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Wednesday, September 24, 2014

Three cursor of Collection : Enumeration, Iterator, ListIterator

Why do we need Cursor?

If we want to retrieve elements from the List one by one , then cursor is required. Java provides three cursor in Collection .

  1. Enumeration
  2. Iterator
  3. ListIterator

Enumeration:

It is available since java 1.0 and can be used only on legacy classes. We can perform only read operation on enumeration object. Enumeration has only two methods.

Methods in enumeration
  •  hasMoreElement() 
  • nextElement()
How to get Enumeration object ?
To get a Enumeration object we should use elements() method from Vector class.

Example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import java.util.*;
class EnumerationDemo 
{
	public static void main(String[] args) 
	{
		Vector v=new Vector();
		v.add("A");
		v.add("B");
		v.add("C");
		Enumeration e=v.elements();
		while(e.hasMoreElements())
		{
			String s=(String)e.nextElement();
			System.out.println(s);
		}
	}
}
Output of the above program:

EnumerationDemo.Java 
Limitation of Enumeration

1.       Can only perform read operation.
2.       Can only be operated on legacy classes.

Iterator

Iterator is a universal cursor and can be applied on any Collection object. Unlike Enumeration it can perform read and remove operations.

Methods of Iterator Interface
1.       hasNext()
2.       next()
3.       remove()

How to get Iterator object ?
To get a Iterator object we should use iterator() method of Collection Interface.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.*;
class IteratorDemo 
{
	public static void main(String[] args) 
	{
		ArrayList al=new ArrayList();
		for(int i=0; i<=10;i++)
		{
			al.add(i);
		}
		Iterator i=al.iterator();
		while(i.hasNext())
		{
			Integer in=(Integer)i.next();
			if(in%2==0)
			{
			System.out.print(in);
			System.out.println(" The number is Even");
			}
			else
			{
				i.remove();
			}			
			
		}
		System.out.println(al);
	}
}

Output of the above program:

IteratorDemo.Java Output

Limitation of Iterator

It is unidirectional cursor hence can only move forward.

ListIterator

ListIterator is given to provide facilities like moving backward and forward in a list, adding an object, replacing an object etc. This cursor can be operated on any List Object. It is a bi-directional cursor.

Methods of ListIterator Interface

public boolean hasNext()


These methods are provided to facilitate the forward direction relation operation.
public Object next()

public int lastIndex()

public boolean hasPrevious()

These methods are provided to facilitate the backward direction relation operation.
public Object previous()

public int previousIndex()

public void add(Object obj)

These methods are provided to facilitate misc. Operations.
public void set(Object obj)

public void remove()

How to get ListIterator object ?
To get a ListIterator object we should use listIterator() method of List Interface.

Example:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
class ListIteratorDemo  
{
	public static void main(String[] args) 
	{
		LinkedList l=new LinkedList();
		l.addFirst("J");
		l.add("A");
		l.add("V");
		l.addLast("A");
		System.out.println(l);
		
		ListIterator li=l.listIterator();
		while(li.hasNext())
		{
			String data=(String)li.next();
			if(data.equals("A"))
			{
				li.set("K");				
			}
		}
		System.out.println(l);
	}
}


Summary of three Cursor


Properties
Enumeration
Iterator
ListIterator
Is legacy ?
Yes
No
No
Access
Can only read
Read and remove
Read, replace, remove, add
How to get object ?
elements() method of Vector class
iterator() method of Collection interface
listIterator() method of List interface.
Direction
Unidirectional
Unidirectional
Bidirectional



No comments: