Java Iterator toMap(final Iterator> iterator)

Here you can find the source of toMap(final Iterator> iterator)

Description

Generics equivalent of commons-collections' IteratorUtils.toMap()

License

Open Source License

Parameter

Parameter Description
iterator The entry iterator to build the map out of
K keys type
V values type

Return

a type-safe

Declaration

public static <K, V> Map<K, V> toMap(final Iterator<Map.Entry<K, V>> iterator) 

Method Source Code


//package com.java2s;
/*//from   www  . ja v  a  2  s  .  c  o  m
 * Copyright (c) 2011, Francis Galiegue <fgaliegue@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashMap;

import java.util.Iterator;
import java.util.Map;

public class Main {
    /**
     * <p>Generics equivalent of commons-collections' IteratorUtils.toMap()</p>
     *
     * @param iterator The entry iterator to build the map out of
     * @param <K> keys type
     * @param <V> values type
     * @return a type-safe {@link HashMap}
     */
    public static <K, V> Map<K, V> toMap(final Iterator<Map.Entry<K, V>> iterator) {
        final Map<K, V> ret = new HashMap<K, V>();
        Map.Entry<K, V> entry;

        while (iterator.hasNext()) {
            entry = iterator.next();
            ret.put(entry.getKey(), entry.getValue());
        }

        return ret;
    }
}

Related

  1. toArray(final Iterator iterator)
  2. toCollection(final Iterator iterator, final Collection c)
  3. toCommaSeparatedString(Iterator i)
  4. toIterator(Collection col)
  5. toIterator(final T[] data)
  6. toObjectIterator(Object[] objects)
  7. toSet(Iterator iteration)
  8. toString(Iterator iterObjects)
  9. toString(Iterator tokens)