enumeration As Iterable - Java Collection Framework

Java examples for Collection Framework:Iterable

Description

enumeration As Iterable

Demo Code

// Licensed to the Apache Software Foundation (ASF) under one
//package com.java2s;
import java.util.Enumeration;
import java.util.Iterator;

public class Main {
    public static <T> Iterable<T> enumerationAsIterable(
            final Enumeration<T> e) {
        return new Iterable<T>() {
            public Iterator<T> iterator() {
                return new Iterator<T>() {
                    public boolean hasNext() {
                        return e.hasMoreElements();
                    }/* w w w  . j a v a 2  s . c om*/

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

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

Related Tutorials