Java Iterable shallowCopy(Iterable ori)

Here you can find the source of shallowCopy(Iterable ori)

Description

Generates a new Iterable that references the items of the given Iterable .

License

Open Source License

Parameter

Parameter Description
T The collection item fieldClass.
ori The iterable to get a shallow copy for.

Return

The shallow copy.

Declaration

public static <T> List<T> shallowCopy(Iterable<T> ori) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**//from  w w w  . ja va2s .c  o  m
     * Generates a new {@link Iterable} that references the items of the given
     * {@link Iterable}.
     *
     * @param <T>
     *          The collection item fieldClass.
     * @param ori
     *          The iterable to get a shallow copy for.
     * @return The shallow copy.
     */
    public static <T> List<T> shallowCopy(Iterable<T> ori) {
        ArrayList<T> copy = new ArrayList<T>();

        for (T t : ori) {
            copy.add(t);
        }

        return copy;
    }

    /**
     * Generates a collection that references the items of the given
     * {@link Iterator}.<br>
     * The iterator should be at its start position.
     *
     * @param <T>
     *          The collection item fieldClass.
     * @param iter
     *          The iterator to get a shallow copy for.
     * @return The shallow copy.
     */
    public static <T> List<T> shallowCopy(Iterator<T> iter) {
        ArrayList<T> copy = new ArrayList<T>();

        while (iter.hasNext()) {
            copy.add(iter.next());
        }

        return copy;
    }
}

Related

  1. prettyPrintComma(Iterable a)
  2. removeElement(final Iterable iterable, int index)
  3. removeIgnoreCase(Iterable haystack, String needle)
  4. secondOf(final Iterable iterable)
  5. sequenceEqual(Iterable one, Iterable two)
  6. toFriendlyString(Iterable iterable, String seperator)
  7. unique(Iterable source, T defaultElement)
  8. valueOfMultiple(Iterable... iterables)