Here you can find the source of copyIntoCollectionFrom(Collection
Parameter | Description |
---|---|
collection | a parameter |
iterable | a parameter |
public static <E> void copyIntoCollectionFrom(Collection<E> collection, Iterable<E> iterable)
//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); } } } }