• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Enumeration and Iterator

February 6, 2013 By j2eereference Leave a Comment

Enumeration:

Enumeration is the part of legacy classes that deals with the objects and it comes with java1.1. Enumeration is used to go through vector, hashtable etc.

Enumeration acts as Read only interface, because it has the methods only to traverse and fetch the objects.  So Enumeration is used when ever we want to make Collection objects as Read-only.

Enumeration has following methods:

hasMoreElements() — Check whether enumeration contains more elements.

nextElement() —  Returns the next element of the enumeration  if the enumeration object has at least one more element to provide.

An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement() method return successive elements of the series.

For example, to print all elements of a vector v:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.j2eereference;
 
import java.util.Enumeration;
import java.util.Vector;
 
public class EnumerationSample {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Ankita");
v.add("Agarwal");
Enumeration en=v.elements();
while(en.hasMoreElements()){
System.out.println(en.nextElement());
}
}
}

Output

Ankita
Agarwal

Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable.

Iterator:

Iterator is introduced in Collection framework. Iterator is used with loops like while loop, for loop etc.

The java.util.Iterator<E> interface provides for one-way traversal

and

java.util.ListIterator<E> provides two-way traversal.

Iterator<E> is a replacement for the older Enumeration class which was used before collections were added to Java.

Iterator has a remove () method, so that we can manipulate the objects like adding and removing the objects from collection.

Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.

Iterator has following methods:

hasNext() —  To determine when all the elements in the container have been visited the hasNext() test method is used. True if there are more elements for the iterator.

next() — The next() method advances the iterator and returns the value pointed to by the iterator. The first element is obtained upon the first call to next().

remove() — remove() method of the iterator removes the most recently visited element from the container while keeping the iterator usable.

Iterator Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.j2eereference;
 
import java.util.Iterator;
import java.util.Vector;
 
public class IteratorSample {
 
public static void main(String[] args) {
Vector v = new Vector();
v.add("Ankita");
v.add("John");
v.add("Peter");
 
Iterator it = v.iterator();
while (it.hasNext()) {
String value = (String) it.next();
System.out.println(value);
it.remove();
System.out.println("Vector size is " + v.size());
}
}
}

Output

Ankita
Vector size is 2
John
Vector size is 1
Peter
Vector size is 0

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.