集册 Java实例教程 合并映射

合并映射

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

494
合并映射


//package com.nowjava;// from 时   代    Java - nowjava.com

import java.util.*;


public class Main {

    public static <T> Map<T, Integer> mergeMaps(

            final Map<T, Integer>... maps) {

        final Map<T, Integer> merged = new HashMap<T, Integer>();

        for (final Map<T, Integer> map : maps) {

            for (final Map.Entry<T, Integer> entry : map.entrySet()) {

                if (entry.getValue() != null && entry.getValue() != 0) {

                    final Integer curValue = merged.get(entry.getKey());

                    merged.put(entry.getKey(),

                            curValue == null ? entry.g
展开阅读全文