Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2006, 2014 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Mike Kucera (IBM Corporation) - initial API and implementation * Sergey Prigogin (Google) * Nathan Ridge *******************************************************************************/ import java.util.Iterator; public class Main { /** * Creates an Iterable instance that just returns the given Iterator from its iterator() method. * * This is useful for using an iterator in a foreach loop directly. * * <p> * Example use: * <pre> * for (Object o : iterable(iterator)) { ... } * </pre> * * @throws NullPointerException if list is {@code null} */ public static <T> Iterable<T> iterable(final Iterator<T> iter) { if (iter == null) throw new NullPointerException("iter parameter is null"); //$NON-NLS-1$ return new Iterable<T>() { @Override public Iterator<T> iterator() { return iter; } }; } }