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. 后序:
大家可以自己尝试着改变形参的类型,自己多练练手。