java 實現合並map示例Demo1


1. 前期准備

① Fastjson

② Guava

*③ eclipse編輯器

*④ JDK1.8

*⑤ Maven構建工程

2. 示例代碼類方法概覽

3. 示例全部代碼

package com.drew.test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author zero 2019/04/17
 */
public class MergeMapOrList {

    /**
     * 根據固定區間值計算范圍:分布區間
     * 
     * @param range 固定區間范圍
     * @param list 整數集合
     * @return 區間map:key為區間,value為list中元素包含在其中的個數。
     * @author zero 2019/04/16
     */
    public static Map<String, Integer> calcToList(int range, List<Integer> list) {
        Map<String, Integer> result = Maps.newHashMap();// 結果:key為區間如[0,30),value為訪問數。
        for (Integer timeKey : list) {
            int start = timeKey / range;
            int end = timeKey / range + 1;
            String key = "[" + (start * range) + "," + (end * range) + ")";
            if (result.containsKey(key)) {
                result.put(key, result.get(key) + 1);
            } else {
                result.putIfAbsent(key, 1);
            }
        }
        return result;
    }

    /**
     * 根據固定區間值計算范圍:分布區間
     * 
     * @param range 區間的跨度
     * @param map key:數據,value:此區間的個數
     * @return map key:區間范圍,value:此區間的個數
     * @author zero 2019/04/16
     */
    public static Map<String, Integer> calcToMap(int range, Map<Integer, Integer> map) {
        Map<String, Integer> result = Maps.newHashMap();// 結果:key為區間如[0,30),value為訪問數。
        for (Integer timeKey : map.keySet()) {
            int start = timeKey / range;
            int end = timeKey / range + 1;
            String key = "[" + (start * range) + "," + (end * range) + ")";
            if (result.containsKey(key)) {
                result.put(key, result.get(key) + map.get(timeKey));
            } else {
                result.putIfAbsent(key, map.get(timeKey));
            }
        }
        return result;
    }

    /**
     * 合並兩個map集合
     * 
     * @param oldMap
     * @param newMap
     * @return
     * @author zero 2019/04/17
     */
    public static Map<String, Integer> mergeMap(Map<String, Integer> oldMap, Map<String, Integer> newMap) {
        if (oldMap == null || oldMap.isEmpty())
            return newMap;
        if (newMap == null || newMap.isEmpty())
            return oldMap;
        Map<String, Integer> tmpMapBigger = Maps.newHashMap();
        Map<String, Integer> tmpMapSmall = Maps.newHashMap();
        if (oldMap.size() < newMap.size()) {
            tmpMapBigger.putAll(newMap);
            tmpMapSmall.putAll(oldMap);
        } else {
            tmpMapBigger.putAll(oldMap);
            tmpMapSmall.putAll(newMap);
        }
        for (String iKey : tmpMapSmall.keySet()) {
            if (tmpMapBigger.containsKey(iKey)) {
                tmpMapBigger.put(iKey, tmpMapBigger.get(iKey) + tmpMapSmall.get(iKey));
            } else {
                tmpMapBigger.put(iKey, tmpMapSmall.get(iKey));
            }
        }
        return tmpMapBigger;
    }

    /**
     * 合並兩個map數據(建議使用)
     * 
     * @param mergedMap
     * @param newMap
     * @return
     * @author zero 2019/04/17
     */
    public static Map<String, Integer> mergeMapPlus(Map<String, Integer> mergedMap, Map<String, Integer> newMap) {
        if (mergedMap.isEmpty() || mergedMap == null)
            return newMap;
        if (newMap.isEmpty() || newMap == null)
            return newMap;
        for (String key : newMap.keySet()) {
            if (mergedMap.containsKey(key)) {
                mergedMap.put(key, mergedMap.get(key) + newMap.get(key));
            } else {
                mergedMap.put(key, newMap.get(key));
            }
        }
        return mergedMap;
    }

    @SafeVarargs
    public static Map<String, Integer> mergeMulMap(Map<String, Integer>... maps) {
        List<Map<String, Integer>> list = Arrays.asList(maps);
        Map<String, Integer> result = Maps.newHashMap();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isEmpty() || list.get(i) == null) {
                continue;
            } else {
                for (String key : list.get(i).keySet()) {
                    if (result.containsKey(key)) {
                        result.put(key, result.get(key) + list.get(i).get(key));
                    } else {
                        result.put(key, list.get(i).get(key));
                    }
                }
            }
        }
        return result;
    }

    public static Map<String, Integer> mergeMulMap2(List<Map<String, Integer>> list) {
        Map<String, Integer> result = Maps.newHashMap();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isEmpty() || list.get(i) == null) {
                continue;
            } else {
                for (String key : list.get(i).keySet()) {
                    if (result.containsKey(key)) {
                        result.put(key, result.get(key) + list.get(i).get(key));
                    } else {
                        result.put(key, list.get(i).get(key));
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println("==================================================");
        int range = 30;// 30秒
        List<Integer> list =
            Lists.newArrayList(0, 1, 3, 4, 30, 31, 45, 67, 87, 90, 94, 100, 150, 1770, 1800, 360000, 360001, 360009);
        System.out.println("原始數據list2Map:\t" + JSON.toJSONString(calcToList(range, list)));
        Map<String, Integer> list2Map = calcToList(range, list);
        Map<Integer, Integer> map = ImmutableMap.of(0, 3, 1, 3, 34, 6, 35, 6, 67, 9);
        System.out.println("原始數據map2Map:\t" + JSON.toJSONString(calcToMap(range, map)));
        Map<String, Integer> map2Map = calcToMap(range, map);
        System.out.println("--------------不同Map合並方法:萬變不離其宗!--------------");
        System.out.println("mergeMulMap:\t" + JSON.toJSONString(mergeMulMap(list2Map, map2Map)));
        System.out.println("mergeMulMap2:\t" + JSON.toJSONString(mergeMulMap2(Arrays.asList(list2Map, map2Map))));
        System.out.println("mergeMap:\t" + JSON.toJSONString(mergeMap(list2Map, map2Map)));
        System.out.println("mergeMapPlus:\t" + JSON.toJSONString(mergeMapPlus(list2Map, map2Map)));
        System.out.println("==================================================");
    }

}

 4. 示例結果展示

5. 后序:

大家可以自己嘗試着改變形參的類型,自己多練練手。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM