集册 Java实例教程 获取包含所提供映射中所有项的映射。

获取包含所提供映射中所有项的映射。

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

500
获取包含所提供映射中所有项的映射。

/*

 * Copyright Terracotta, Inc.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

//package com.nowjava;

import java.util.ArrayList;

/**
时 代 J     a    v  a - nowjava.com 提供 
**/

import java.util.HashMap;

import java.util.HashSet;


import java.util.List;

import java.util.Map;

import java.util.Set;


public class Main {

    /**

     * Gets a {@code Map} containing all of the entries from the maps provided.

     *

     * @param map1 the first {@code Map} instance to combine

     * @param map2 the second {@code Map} instance to combine

     *

     * @return a new {@code Map} containing all of the entries

     */

    static Map<String, String> union(final Map<String, String> map1,

            final Map<String, String> map2) {

        final List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

        maps.add(map1);

        maps.add(map2);

        return unionMaps(maps);

    }

    /**
    时 代 J     a    v  a - nowjava.com
    **/

    /**

     * Gets a {@code Map} containing all of the entries from the maps provided.

     *

     * @param map1 the first {@code Map} instance to combine

     * @param map2 the second {@code Map} instance to combine

     * @param map3 the third {@code Map} instance to combine

     *

     * @return a new {@code Map} containing all of the entries

     */

    static Map<String, String> union(final Map<String, String> map1,

            final Map<String, String> map2, final Map<String, String> map3) {

        final List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

        maps.add(map1);

        maps.add(map2);

        maps.add(map3);

        return unionMaps(maps);

    }


    /**

     * Gets a {@code Set} containing all of the elements from the sets provided

     *

     * @param set1 the first {@code Set} instance to combine

     * @param set2 the second {@code Set} instance to combine

     *

     * @return a new {@code Set} containing all of the elements

     */

    static Set<String> union(final Set<String> set1, final Set<String> set2) {

        final List<Set<String>> sets = new ArrayList<Set<String>>();

        sets.add(set1);

        sets.add(set2);

        return unionSets(sets);

    }


    /**

     * Gets a {@code Map} containing all of the entries from the maps provided.

     *

     * @param maps the {@code Map} instances to combine

     *

     * @return a new {@code Map} containing all of the entries

     */

    private static Map<String, String> unionMaps(

            final List<Map<String, String>> maps) {

        final Map<String, String> union = new HashMap<String, String>();

        for (Map<String, String> map : maps) {

            union.putAll(map);

        }

        
展开阅读全文