Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Making Enumerations Iterable - JDK 5 Example

Making Enumerations Iterable - tutorial & newsletter of Making Enumerations Iterable.

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

Enumeration's Iteration

In this section, you will learn how we can make Enumeration iterable. The best way to do this to create an adapter for the Enumeration. And for fulfilling iterable interface, create an IterableEnumeration which is Generic enabled.

Example

In the given below example We are going to make an Enumeration iterable :

package simpleCoreJava;

import java.util.*;

public class IterableEnumeration<T> implements Iterable<T> {
private final Enumeration<T> en;

public IterableEnumeration(Enumeration<T> en) {
this.en = en;
}

// return an adaptor for the Enumeration
public Iterator<T> iterator() {
return new Iterator<T>() {
public boolean hasNext() {
return en.hasMoreElements();
}

public T next() {
return en.nextElement();
}

public void remove() {
throw new UnsupportedOperationException();
}
};
}
}

Second Way

The second way for using it is by making an instance of the iterableEnumeration and also giving it generic type as below :

import java.sql.*;
import java.util.*;

public class IterableTest {
public static void main(String[] args) {
Vector<String> sv = new Vector<String>();
sv.addElement("Maximilian");
sv.addElement("Francis");
sv.addElement("Kabutz");

// using the generics makes it look a bit clumsy
IterableEnumeration<String> ie =
new IterableEnumeration<String>(sv.elements());
for (String s : ie) {
System.out.println(s);
}

// Without generics, we cannot automatically cast to String
IterableEnumeration ie2 =
new IterableEnumeration(sv.elements());
for (Object s : ie2) { // here we now have to use Object type
System.out.println(s);
}

// Again, generics makes the code look clumsy
// here you should load your own driver, if applicable
new sun.jdbc.odbc.JdbcOdbcDriver();
IterableEnumeration<Driver> drivers =
new IterableEnumeration<Driver>(
DriverManager.getDrivers());
for (Driver driver : drivers) {
System.out.println("driver = " + driver.getClass());
}

// or we could build up the list using Collections.list()
// and iterate through that - this is ineffient.
for (Driver driver : Collections.list(
DriverManager.getDrivers())) {
System.out.println("driver = " + driver.getClass());
}
}
}

 

Advertisements

Online Training Classes

Share on Google+Share on Google+

Making Enumerations Iterable - JDK 5 Example

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Making Enumerations Iterable - JDK 5 Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads