2 sinat 25272999 sinat_25272999 于 2016.04.15 12:21 提问

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个回答

u012850795
u012850795   2016.04.15 14: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;
    }
}
u013321534
u013321534   2016.04.15 14:43

public class CSDNTest
{
public static void main(String[] args)
{
Map m1 = createMap(3);
Map m2 = createMap(5);
Map m3 = createMap(7);

    Set<String> keySet = new HashSet<String>();

    keySet.addAll(m1.keySet());
    keySet.addAll(m2.keySet());
    keySet.addAll(m3.keySet());

    Map<String, String[]> data = new HashMap<String, String[]>();

    for(String key : keySet)
    {
        String[] array = data.get(key);

        if(null == array)
        {
            array = new String[3];
            data.put(key, array);
        }

        array[0] = m1.get(key);
        array[1] = m2.get(key);
        array[2] = m3.get(key);

        for(int i=0;i<array.length;i++)
        {
            if(null == array[i])
            {
                array[i] = "-1";
            }
        }
    }

    for(String s : data.keySet())
    {
        System.out.println("key:"+s+"  value:"+Arrays.toString(data.get(s)));
    }
}

public static Map<String, String> createMap(int length)
{
    String key = "key";
    String value = "value";
    Map<String, String> map = new HashMap<String, String>();
    for(int j=1;j<=length;j++)
    {
        map.put(key + j, value + j);
    }

    return map;
}

}

输出:
key:key4 value:[-1, value4, value4]
key:key3 value:[value3, value3, value3]
key:key6 value:[-1, -1, value6]
key:key5 value:[-1, value5, value5]
key:key2 value:[value2, value2, value2]
key:key1 value:[value1, value1, value1]
key:key7 value:[-1, -1, value7]

luoyongsir
luoyongsir   2016.04.15 20:47

用谷歌 Guava 中的Multimap 。
http://ifeve.com/google-guava-newcollectiontypes/

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!