Java Iterator putAllFromAll(Map map, Iterator> mapsIterator)

Here you can find the source of putAllFromAll(Map map, Iterator> mapsIterator)

Description

Puts all entries from all maps in the range of an iterator to a given map.

License

Open Source License

Parameter

Parameter Description
map the map
mapsIterator the maps iterator
K the type of keys
V the type of values

Return

the given map with all entries from all maps in interator's range

Declaration

public static <K, V> Map<K, V> putAllFromAll(Map<K, V> map, Iterator<Map<K, V>> mapsIterator) 

Method Source Code

//package com.java2s;
/*//from w ww  .jav  a2  s. c  o m
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-util nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.util.Iterator;

import java.util.Map;

public class Main {
    /**
     * Puts all entries from all maps in the range of an iterator to a given map.
     * @param map the map
     * @param mapsIterator the maps iterator
     * @param <K> the type of keys
     * @param <V> the type of values
     * @return the given map with all entries from all maps in interator's range
     */
    public static <K, V> Map<K, V> putAllFromAll(Map<K, V> map, Iterator<Map<K, V>> mapsIterator) {
        for (Map<K, V> eachMap : in(mapsIterator)) {
            map.putAll(eachMap);
        }
        return map;
    }

    /**
     * Adapts an {@link Iterator} to an {@link Iterable} for use in enhanced for
     * loops. If {@link Iterable#iterator()} is invoked more than once, an
     * {@link IllegalStateException} is thrown.
     * @param iterator the iterator
     * @param <T> the type of elements
     * @return the iterable for iterator
     */
    public static <T> Iterable<T> in(final Iterator<T> iterator) {
        assert iterator != null;
        class SingleUseIterable implements Iterable<T> {
            private boolean used = false;

            @Override
            public Iterator<T> iterator() {
                if (used) {
                    throw new IllegalStateException("SingleUseIterable already invoked");
                }
                used = true;
                return iterator;
            }
        }
        return new SingleUseIterable();
    }

    /**
     * @param map
     *        a map.
     * @param keysAndValues
     *            a sequence of key and value pairs to be placed into a given Map.
     * 
     * @return the received arguments (interpreted as a sequence of key and
     *         value pairs) in a given map.
     * @param <K>
     *            the type of the Map's key.
     * @param <V>
     *            the type of the Map's value.
     */
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> putAll(Map<K, V> map, Object... keysAndValues) {
        int i = 0;
        while (i != keysAndValues.length) {
            map.put((K) keysAndValues[i], (V) keysAndValues[i + 1]);
            i += 2;
        }
        return map;
    }
}

Related

  1. printElements(String desc, Iterator i)
  2. printIterator(final Iterator iterator)
  3. printIterator(Iterator i, String header)
  4. printIterator(Iterator> it)
  5. put(String key, Iterator value)
  6. read(Iterator it, E[] array)
  7. reduceSet(Iterator> values)
  8. removeAll(Collection collection, Iterator itemsToRemove)
  9. removeObject(Object item, Iterator iter, boolean justFirst)