Java Iterator to Iterable toIterable(final Iterator iterator)

Here you can find the source of toIterable(final Iterator iterator)

Description

Returns an Iterable returning the passed in Iterator .

License

Open Source License

Parameter

Parameter Description
T the generic type of the passed in Iterator and for the returned Iterable.
iterator the Iterator that will be returned by the Iterable.

Return

an Iterable returning the passed in Iterator

Declaration

public static <T> Iterable<T> toIterable(final Iterator<T> iterator) 

Method Source Code

//package com.java2s;
/*//from  www.j a va  2 s  .  c  om
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2015 ForgeRock AS
 */

import java.util.*;

public class Main {
    /**
     * Returns an {@link Iterable} returning the passed in {@link Iterator}. THis
     * allows using methods returning Iterators with foreach statements.
     * <p>
     * For example, consider a method with this signature:
     * <p>
     * <code>public Iterator&lt;String&gt; myIteratorMethod();</code>
     * <p>
     * Classical use with for or while loop:
     *
     * <pre>
     * for (Iterator&lt;String&gt; it = myIteratorMethod(); it.hasNext();)
     * {
     *   String s = it.next();
     *   // use it
     * }
     *
     * Iterator&lt;String&gt; it = myIteratorMethod();
     * while(it.hasNext();)
     * {
     *   String s = it.next();
     *   // use it
     * }
     * </pre>
     *
     * Improved use with foreach:
     *
     * <pre>
     * for (String s : StaticUtils.toIterable(myIteratorMethod()))
     * {
     * }
     * </pre>
     *
     * </p>
     *
     * @param <T>
     *          the generic type of the passed in Iterator and for the returned
     *          Iterable.
     * @param iterator
     *          the Iterator that will be returned by the Iterable.
     * @return an Iterable returning the passed in Iterator
     */
    public static <T> Iterable<T> toIterable(final Iterator<T> iterator) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return iterator;
            }
        };
    }
}

Related

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