集册 Java实例教程 转换密钥列表

转换密钥列表

欢马劈雪     最近更新时间:2020-01-02 10:19:05

517
将键值对的列表转换为映射,并始终插入第一个重复键,或始终插入最后一个重复键。

/*******************************************************************************

 * Copyright (c) 2008, 2010 Xuggle Inc.  All rights reserved.

 *  

 * 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/>.

 *******************************************************************************//** from N o w J a v a . c o m - 时代Java**/

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)/** 来 自 nowjava.com - 时代Java**/

            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);

            }

        }

    }

    
展开阅读全文