IteratorEnumeration.java Source code

Java tutorial

Introduction

Here is the source code for IteratorEnumeration.java

Source

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

class IteratorEnumeration<E> implements Enumeration<E> {
    private final Iterator<E> iterator;

    public IteratorEnumeration(Iterator<E> iterator) {
        this.iterator = iterator;
    }

    public E nextElement() {
        return iterator.next();
    }

    public boolean hasMoreElements() {
        return iterator.hasNext();
    }

}

public class Main {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
        set.add("a");
        set.add("b");
        set.add("c");
        set.add("d");
        Enumeration<String> x = new IteratorEnumeration<String>(set.iterator());
        while (x.hasMoreElements()) {
            System.out.println(x.nextElement());
        }
    }
}