Converts a list of key-value pairs into a Map , with either the first duplicate key always being inserted, or the last duplicate key always being inserted. - Java Collection Framework

Java examples for Collection Framework:Map

Description

Converts a list of key-value pairs into a Map , with either the first duplicate key always being inserted, or the last duplicate key always being inserted.

Demo Code

/*******************************************************************************
 * Copyright (c) 2008, 2010 Xuggle Inc.  All rights reserved.
 *  //from   w w w.  j  av a  2 s. c  o  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 list of key-value pairs into a {@link Map}, with either
     * the first duplicate key always being inserted, or the last duplicate key
     * always being inserted.
     * 
     * @param list
     *          The list of name value pairs to convert.
     * @param mapToFill
     *          The map to fill. This method will empty the list first.
     * @param mode
     *          How duplicate values in <code>list</code> should be treated.
     */
    public static void listToMap(final List<? extends IKeyValuePair> list,
            final Map<String, String> mapToFill, final ListToMapMode mode) {
        if (list == null)
            throw new IllegalArgumentException();
        if (mapToFill == null)
            throw new IllegalArgumentException();
        mapToFill.clear();
        for (IKeyValuePair pair : list) {
            if (pair != null) {
                final String key = pair.getKey();
                final String value = pair.getValue();
                if (mode == ListToMapMode.FIRST_WINS) {
                    if (mapToFill.containsKey(key))
                        continue;
                }
                if (mode == ListToMapMode.FIRST_NONEMPTY_WINS) {
                    if (mapToFill.containsKey(key)) {
                        final String oldValue = mapToFill.get(key);
                        if (oldValue != null && oldValue.length() > 0)
                            // the list has a non-null value; don't reset it.
                            continue;
                    }
                }
                if (mode == ListToMapMode.LAST_NONEMPTY_WINS) {
                    if (value == null || value.length() == 0) {
                        if (mapToFill.containsKey(key))
                            // already contains an empty key
                            continue;
                    }
                }
                mapToFill.put(key, value);
            }
        }
    }
    /**
     * Converts a list of key-value pairs into a {@link Map}, with either
     * the first duplicate key always being inserted, or the last duplicate key
     * always being inserted.
     * 
     * @param list
     *          The list of name value pairs to convert.
     * @param mode
     *          How duplicate values in <code>list</code> should be treated.
     * @return a new {@link Map} of key-value pairs.
     */
    public static Map<String, String> listToMap(
            final List<? extends IKeyValuePair> list,
            final ListToMapMode mode) {
        final Map<String, String> retval = new HashMap<String, String>();
        listToMap(list, retval, mode);
        return retval;
    }
}

Related Tutorials