sinat_25272999 2016-04-15 04:21 采纳率: 0%
浏览 3592

java求几个hashmap中key的并集,输出value

现有几个hashmap,可能有相同的key,但是value不同,求key的并集,
并输出在不同hashmap中的不同value,若不存在则为-1

比如hashmap1{(我,1),(你,2),(他,3),(它,4)}
hashmap2{(猴,1)(狗,3)(你,4)(他,5)}
hashmap3{(猴,10)(牛,2)(狗,5)(猪,4),(它,8)}
hashmap4{(我,5),(牛,6)}
输出为
key hashmap1 hashmap2 hashmap3 hashmap4
我 1 -1 -1 5
你 2 4 -1 -1
他 3 5 -1 -1
它 4 -1 8 -1
猴 -1 1 10 -1
狗 -1 3 5 -1
牛 -1 -1 2 6
猪 -1 -1 4 -1

  • 写回答

3条回答 默认 最新

  • 寒心孤瞳 2016-04-15 06:20
    关注

    思路是挨个挨个Map遍历,将结果放入一个汇总的map,希望有更漂亮的方案

    
    import java.util.*;
    
    public class MapMerge {
        public static void main(String[] args) {
            List<HashMap<String,Integer>> list = generate();
            HashMap<String,String> result = new HashMap<>();
            HashMap<String,Integer> tmpmap = new HashMap<>();
            //循环遍历每个Map,合并成一个大的map
            for (int i = 0; i < list.size(); i++) {
                //遍历Map中的每一个元素
                tmpmap = list.get(i);
                if(tmpmap==null||tmpmap.size()<1){
                    continue;
                }
                Iterator<String> it = tmpmap.keySet().iterator();
                while(it.hasNext()){
                    String key = it.next();
                    Integer value = tmpmap.get(key);
                    //如果在结果中已经存在key,
                    if(result.containsKey(key)){
                        //该键已经统计过了
                    }else{
                        //前i个hashmap里面没有值,把剩下的hashmap的值挨个取出.
                        StringBuilder sb = new StringBuilder();
                        for (int j = 0; j < i; j++) {
                            sb.append(";-1");
                        }
    
                        for (int j = i; j < list.size(); j++) {
                            sb.append(";"+getValue(list.get(j),key));
                        }
                        sb.deleteCharAt(0);
                        result.put(key, sb.toString());
                    }
                }
            }
            System.out.println("map合并以后:\n" + result);      
        }
    
        private static int getValue(HashMap<String,Integer> map,String key){
            Integer result = map.get(key);
            return result==null?-1:result;
        }
    
        //随机构造n个hashmap
        public static List<HashMap<String,Integer>> generate(){
            List<HashMap<String,Integer>> list = new ArrayList<>();
            Random r = new Random();
            int i=(int) (Math.round(Math.random()*3 + 3));//hashMap的数量
            System.out.println("输入hashMap的数量:   "+i);
    
            int tmpSize = 0;
            HashMap tmpmap = null;
            for (int j = 0; j < i; j++) {
                tmpmap = new HashMap<String,Integer>();
                //hashMap的长度
                tmpSize = (int) (Math.round(Math.random()*10 + 4));
                for (int k = 0; k < tmpSize; k++) {
                    //随机键值对
                    String key = String.valueOf((char)(int) (Math.round(Math.random()*25 + 97)));
                    int value = (int) (Math.round(Math.random()*8 + 1));
                    tmpmap.put(key, value);
                }
                list.add(tmpmap);
                System.out.println(tmpmap);
            }
            return list;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题