Java Collection Copy copyIntoCollectionFrom(Collection collection, Iterable iterable)

Here you can find the source of copyIntoCollectionFrom(Collection collection, Iterable iterable)

Description

Copies all elements from the given Iterable into the given Collection

License

Apache License

Parameter

Parameter Description
collection a parameter
iterable a parameter

Declaration

public static <E> void copyIntoCollectionFrom(Collection<E> collection, Iterable<E> iterable) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Danny Kunz/*from www  .  ja  va  2 s .  co m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Copies all elements from the given {@link Iterable} into the given {@link Collection}
     * 
     * @param collection
     * @param iterable
     */
    public static <E> void copyIntoCollectionFrom(Collection<E> collection, Iterable<E> iterable) {
        if (collection != null && iterable != null) {
            for (E element : iterable) {
                collection.add(element);
            }
        }
    }

    /**
     * Copies all elements from the given {@link Iterable} into the given {@link Collection}
     * 
     * @param collection
     * @param iterable
     * @param maxNumberOfElements
     */
    public static <E> void copyIntoCollectionFrom(Collection<E> collection, Iterable<E> iterable,
            int maxNumberOfElements) {
        if (collection != null && iterable != null) {
            //
            final Iterator<E> iterator = iterable.iterator();
            copyIntoCollectionFrom(collection, iterator, maxNumberOfElements);
        }
    }

    /**
     * Copies all elements from the given {@link Iterator} into the given {@link Collection} <br>
     * This traverses the {@link Iterator}
     * 
     * @param collection
     * @param iterator
     */
    public static <E> void copyIntoCollectionFrom(Collection<E> collection, Iterator<E> iterator) {
        if (collection != null && iterator != null) {
            while (iterator.hasNext()) {
                final E element = iterator.next();
                collection.add(element);
            }
        }
    }

    /**
     * Copies all elements from the given {@link Iterator} into the given {@link Collection}.This traverses the {@link Iterator}
     * only as far as necessary.
     * 
     * @param collection
     * @param iterator
     * @param maxNumberOfElements
     */
    public static <E> void copyIntoCollectionFrom(Collection<E> collection, Iterator<E> iterator,
            int maxNumberOfElements) {
        if (collection != null && iterator != null) {
            for (int ii = 0; ii < maxNumberOfElements && iterator.hasNext(); ii++) {
                final E element = iterator.next();
                collection.add(element);
            }
        }
    }
}

Related

  1. copy(final Collection in)
  2. copy(final Collection collection)
  3. copy(final Collection collection)
  4. copy2Collection(Object[] strs)
  5. copyColl(Collection out, Iterable set)
  6. copyNSorted(final Collection source, final Collection dest, final Comparator order, final int n)
  7. copyRemainder(final T[] sourceArray, final int startIndex, final Collection collector)
  8. copyStringCollection(Collection strings)
  9. copyWithoutNull(Collection orig)