Java Iterator addAll(final C collection, final Iterator iter)

Here you can find the source of addAll(final C collection, final Iterator iter)

Description

adds all elements from iter to collection

License

Apache License

Parameter

Parameter Description
T a parameter
C a parameter
collection a parameter
iter a parameter

Declaration

public static <T, C extends Collection<T>> C addAll(final C collection, final Iterator<T> iter) 

Method Source Code

//package com.java2s;
/**//from  w w  w .j  av a  2s  .  c o  m
 * Copyright 2010 Molindo GmbH
 *
 * 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 {
    /**
     * adds all elements from iter to collection
     * 
     * @param <T>
     * @param <C>
     * @param collection
     * @param iter
     * @return
     */
    public static <T, C extends Collection<T>> C addAll(final C collection, final Iterator<T> iter) {
        while (iter.hasNext()) {
            collection.add(iter.next());
        }
        return collection;
    }

    /**
     * 
     * @param <T>
     * @param iter
     * @return the next element if available, <code>null</code> otherwise
     */
    public static <T> T next(final Iterator<T> iter) {
        return iter == null || !iter.hasNext() ? null : iter.next();
    }
}

Related

  1. addAll(Collection dest, Iterator src)
  2. addAll(Collection dest, Iterator src)
  3. addAll(final Collection targetCollection, final Iterator sourceIterator)
  4. addAll(Iterator iteratorFrom, Collection collectionTo)
  5. addAll(Iterator source, U target)
  6. addAll(List target, Iterator source)