Converts a map into a List of IKeyValuePair objects. - Java Collection Framework

Java examples for Collection Framework:Map

Description

Converts a map into a List of IKeyValuePair objects.

Demo Code

/*******************************************************************************
 * Copyright (c) 2008, 2010 Xuggle Inc.  All rights reserved.
 *  /*from  w  ww  . java  2s .co m*/
 * This file is part of Xuggle-Utils.
 *
 * Xuggle-Utils is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Xuggle-Utils 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Xuggle-Utils.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Main{
    /**
     * Converts a map into a {@link List} of {@link IKeyValuePair} objects.
     * @param map Map to convert.
     * @param listToFill List to fill.  The list has {@link List#clear()} called
     *   before any items are added.
     */
    public static void mapToList(final Map<String, String> map,
            final List<IKeyValuePair> listToFill) {
        if (map == null || listToFill == null)
            throw new IllegalArgumentException();
        final Set<Entry<String, String>> entries = map.entrySet();
        for (Entry<String, String> entry : entries) {
            final String name = entry.getKey();
            final String value = entry.getValue();
            if (name == null)
                continue;
            IKeyValuePair pair = new KeyValuePair(name, value);
            listToFill.add(pair);
        }
    }
    /**
     * Converts a map into a {@link List} of {@link IKeyValuePair} objects, and
     * returns the new list.
     * @param map Map to convert.
     * @return A new {@link List} containing all key-value pairs in <code>map</code>
     *   as {@link IKeyValuePair} objects.
     */
    public static List<IKeyValuePair> mapToList(
            final Map<String, String> map) {
        final List<IKeyValuePair> retval = new ArrayList<IKeyValuePair>();
        mapToList(map, retval);
        return retval;
    }
}

Related Tutorials