对比两个超大List列表中对象,返回相同值和不同值

eg:
对象Message{String msgNo,int fee}
ListA,ListB分别有上亿条,如何对比返回两个list中msgNo相同,fee不同的对象diffFeeList;msgNo及fee均相同的sameList,ListA中msgNo在ListB中不存在的onlyExistAList,ListB中msgNo在ListA中不存在的onlyExistBList

为什么不放在数据库里面去处理,而要用List处理?

首先肯定得采取分治策略,将大List拆成多个小List,然后将每一份List中的String,做字典序排列。然后就可以提高查询的速度

这么大的数据量用数据库啊

这数据量太大了,建议用数据库,效率会好N倍

转成map再处理。。前提是每个对象msgNo不一样。。有相同的话这种方法就不适用了

        List<Message> listA = new ArrayList<Message>();
        List<Message> listB = new ArrayList<Message>();
        Map<String,Integer> mapA = new HashMap<String,Integer>();
        Map<String,Integer> mapB = new HashMap<String,Integer>();
        Map<String,Integer> onlyExistA = new HashMap<String,Integer>();
        Map<String,Integer> onlyExistB = new HashMap<String,Integer>();
        Map<String,Integer> same = new HashMap<String,Integer>();

        for (Message a : listA) {
            mapA.put(a.getMsgNo,a.getFee);
        }
        for (Message b : listB) {
            mapB.put(b.getMsgNo,b.getFee);
        }

        Set<String> setA = mapA.keySet();
        //得到只存在A中的
        for (String aone : setA) {
            if (!mapB.containsKey(aone)) {
                onlyExistA.put(aone, mapA.get(aone));
                mapA.remove(aone);
            }
        }
        Set<String> setB = mapB.keySet();
        //得到只存在B中的
        for (String bone : setB) {
            if (!mapA.containsKey(bone)) {
                onlyExistB.put(bone, mapB.get(bone));
                mapB.remove(bone);
            }
        }
        //此时setA和setB中内容是一样的,不一样的都被剔除出去
        Set<String> set = mapA.keySet();
        for (String one : set) {
            if (mapA.get(one).equals(mapB.get(one))) {
                same.put(one,mapA.get(one));
                mapA.remove(one);
                mapB.remove(one);
            }
        }
        //最后剩下的mapA和mapB中就是msgNo相同,fee不同的

上亿条,话说这么大的数据量,一次性全对比出来的语言大概没有,我用java,一秒for循环也才动作一两千次,还是很好的机器,建议你放到数据库里,分段读取对比,上亿条的对比量,好点的电脑应该都要一两天的工作量才行

对了,还有一个思路,使用数据库的多表联合查询,设置条件,这样效率会提高很多,大概也就几分钟能对比出来