Java Iterator to Iterable iterable(final Iterator iter)

Here you can find the source of iterable(final Iterator iter)

Description

Creates an Iterable instance that just returns the given Iterator from its iterator() method.

License

Open Source License

Exception

Parameter Description
NullPointerException if list is null

Declaration

public static <T> Iterable<T> iterable(final Iterator<T> iter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2010 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:/*from w  w  w  .j ava 2  s. c om*/
 *     Mike Kucera (IBM Corporation) - initial API and implementation
 *******************************************************************************/

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.
     * 
     * e.g.
     * 
     * for(Object o : iterable(list.listIterator())) {
     *     // do something
     * }
     * 
     * @throws NullPointerException if list is 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>() {
            public Iterator<T> iterator() {
                return iter;
            }
        };
    }
}

Related

  1. iterable(final Iterator it)
  2. iterable(final Iterator iterator)
  3. iterable(final Iterator iterator)
  4. iterable(Iterator iterator)
  5. toIterable(final Iterator iterator)
  6. toIterable(final Iterator iterator)
  7. toIterable(final Iterator iterator)