关于两个List的比较

来源:互联网 发布:淘宝上的同款货源 编辑:程序博客网 时间:2024/05/13 16:19

1. java自带的:

List<String> list1 = new ArrayList<String>();List<String> list2 = new ArrayList<String>();list1.add("g");list1.add("s");list1.add("a");list1.add("f");list2.add("g");list2.add("c");list2.add("b");list2.add("a");List<String> list3=new ArrayList<>();list3.addAll(list1);// 这是交集list1.retainAll(list2);System.out.println("交集》》"+list1);list3.removeAll(list1);System.out.println("list1排除交集后》》"+list3);list2.removeAll(list1);System.out.println("list2排除交集后》》"+list2); String str=  String.format(">>adsf>>%s<<<<%s","2","3");System.out.println(str);

2. 自己写的

   

private void allfor(int nowbianhaosx, int lastbianhao, PushApprovalWorkFlowModel flow) {    List s1 = pushApprovalWorkFlowRepository.findValueModelList(nowbianhaosx);    List s2 = pushApprovalWorkFlowRepository.findValueModelList(lastbianhao);    Map sm1 = new HashMap();    Map sm2 = new HashMap();    List<Map> commonMap = new ArrayList<>();    List<Map> deleteMap = new ArrayList<>();    List<Map> addMap = new ArrayList<>();    int i = 0;    int j = 0;    String s = null;    a:    for (i = 0; i < s1.size(); i++) {        sm1 = (Map) s1.get(i);        int sm1product = Integer.parseInt(sm1.get("productId").toString());        int sm1comid = Integer.parseInt(sm1.get("comboId").toString());        int sm1id = Integer.parseInt(sm1.get("编号").toString());        int sm1sum = Integer.parseInt(sm1.get("数量").toString());        String sm1price = sm1.get("折后价").toString();        for (j = 0; j < s2.size(); j++) {            sm2 = (Map) s2.get(j);            int sm2product = Integer.parseInt(sm2.get("productId").toString());            int sm2comid = Integer.parseInt(sm2.get("comboId").toString());            int sm2id = Integer.parseInt(sm2.get("编号").toString());            int sm2sum = Integer.parseInt(sm2.get("数量").toString());            String sm2price = sm2.get("折后价").toString();            if (sm1product == sm2product && sm1comid == sm2comid) {                commonMap.add(sm1);                //System.out.println(s1[i] + "标记为相同");                List<报价精品详细表精品Model> youmeiyoulist =                        repository报价精品详细精品表.findByThree(sm2product, sm2comid, sm2id);                if (youmeiyoulist.size() > 0) {                    if (sm2sum == sm1sum && sm2price.equals(sm1price)) {                        int ids = youmeiyoulist.get(0).getId();                        报价精品详细表精品Model mlist1 = repository报价精品详细精品表.findOne(ids);                        mlist1.set编号(sm1id);                        // mlist1.set备注("正向 :当 上一次已经插入且+没有 修改 数量 和折后价 +这次更新+编号 状态不变");                        repository报价精品详细精品表.save(mlist1);                    } else {                        int idss2 = youmeiyoulist.get(0).getId();                        报价精品详细表精品Model mlists2 = repository报价精品详细精品表.findOne(idss2);                        mlists2.set编号(sm1id);                        mlists2.set数量(sm1sum);                        mlists2.setOrderStatus("5");                        //  mlists2.set备注("正向 : 当 上一次已经插入且+有 修改 数量 和折后价 +这次更新+编号+ 状态5 ");                        repository报价精品详细精品表.save(mlists2);                        saveMsgByXiaoshouGuwenToAllJpzhuanyuan(nowbianhaosx, flow);                    }                } else {                    if (sm2sum == sm1sum && sm2price.equals(sm1price)) {                        报价精品详细表精品Model mlist3a = new 报价精品详细表精品Model();                        mlist3a.setOrderStatus("1");                        mlist3a.set数量(sm1sum);                        mlist3a.set编号(sm1id);                        mlist3a.setComboId(sm1comid);                        mlist3a.setProductId(sm1product);                        //mlist3a.set备注("正向 :当 上一次没插入且+没有 修改 数量 和折后价 +这次更新+编号 状态1");                        repository报价精品详细精品表.save(mlist3a);                    } else {                        报价精品详细表精品Model mlist3a = new 报价精品详细表精品Model();                        mlist3a.setOrderStatus("5");                        mlist3a.set数量(sm1sum);                        mlist3a.set编号(sm1id);                        mlist3a.setComboId(sm1comid);                        mlist3a.setProductId(sm1product);                        // mlist3a.set备注("正向 :当 上一次没插入且+ 这一次 修改 数量 和折后价 +这次插入 +编号 状态5");                        repository报价精品详细精品表.save(mlist3a);                        saveMsgByXiaoshouGuwenToAllJpzhuanyuan(nowbianhaosx, flow);                    }                }                continue a;            }        }        if (j == s2.size()) {            addMap.add(sm1);            // System.out.println(s1[i] + "标记为新增");            报价精品详细表精品Model mlist3as = new 报价精品详细表精品Model();            mlist3as.setOrderStatus("1");            mlist3as.set数量(sm1sum);            mlist3as.set编号(sm1id);            mlist3as.setComboId(sm1comid);            mlist3as.setProductId(sm1product);            // mlist3as.set备注("正向 :当 上一次压根就 没有 这个精品   这一次插入 ");            repository报价精品详细精品表.save(mlist3as);            saveMsgByXiaoshouGuwenToAllJpzhuanyuan(nowbianhaosx, flow);        }    }    List s3 = pushApprovalWorkFlowRepository.findValueModelList(nowbianhaosx);    int newbianhao = 0;    if (s3.size() > 0) {        Map bianhaom = (Map) s3.get(0);        String bianhaoms = bianhaom.get("精品").toString();        newbianhao = Integer.parseInt(bianhaoms);    }    List s4 = pushApprovalWorkFlowRepository.findValueModelList(lastbianhao);    Map sm3 = new HashMap();    Map sm4 = new HashMap();    int ij = 0;    int jj = 0;    b:    for (ij = 0; ij < s4.size(); ij++) {        sm4 = (Map) s4.get(ij);        int sm4product = Integer.parseInt(sm4.get("productId").toString());        int sm4comid = Integer.parseInt(sm4.get("comboId").toString());        int sm4id = Integer.parseInt(sm4.get("编号").toString());        int sm4sum = Integer.parseInt(sm4.get("数量").toString());        String sm1price = sm1.get("折后价").toString();        for (jj = 0; jj < s3.size(); jj++) {            sm3 = (Map) s3.get(jj);            int sm3product = Integer.parseInt(sm3.get("productId").toString());            int sm3comid = Integer.parseInt(sm3.get("comboId").toString());            int sm3id = Integer.parseInt(sm3.get("编号").toString());            int sm3sum = Integer.parseInt(sm3.get("数量").toString());            String sm3price = sm2.get("折后价").toString();            if (sm4product == sm3product && sm3comid == sm4comid) {                commonMap.add(sm4);                //System.out.println(s1[i] + "标记为相同");                continue b;            }        }        if (jj == s3.size()) {            deleteMap.add(sm4);            //System.out.println(s2[i] + "标记为删除");            List<报价精品详细表精品Model> youmeiyoulists = repository报价精品详细精品表.findByThree(sm4product, sm4comid, sm4id);            if (youmeiyoulists.size() > 0) {                int ids1 = youmeiyoulists.get(0).getId();                报价精品详细表精品Model mlist7 = repository报价精品详细精品表.findOne(ids1);                mlist7.set编号(newbianhao);                mlist7.setOrderStatus("9");                mlist7.set备注("反向:这是 上一次 已经插入了+ 所以 现在只更新 +改编号,+ 状态 9 ");                repository报价精品详细精品表.save(mlist7);                saveMsgByXiaoshouGuwenToAllJpzhuanyuan(nowbianhaosx, flow);            } else {                报价精品详细表精品Model mlist3as = new 报价精品详细表精品Model();                mlist3as.set编号(newbianhao);                mlist3as.setOrderStatus("9");                mlist3as.setComboId(sm4comid);                mlist3as.setProductId(sm4product);                mlist3as.set数量(sm4sum);                mlist3as.set备注("反向: 这是 上一次 没有插入了+ 所以 现在插入 +现在的编号+ 以前的 conid + productId,筛选出 已经删除了的+ 状态 9 ");                repository报价精品详细精品表.save(mlist3as);            }        }    }}

3. 简化为:

List<String> list1 = new ArrayList<String>();List<String> list2 = new ArrayList<String>();list1.add("1");list1.add("2");list1.add("3");list1.add("4");list2.add("1");list2.add("5");list2.add("6");list2.add("7");List<String> comm = new ArrayList<String>();List<String> delete = new ArrayList<String>();List<String> update = new ArrayList<String>();List<String> addmap = new ArrayList<String>();int i = 0;int j=0;a:for(  i = 0; i < list1.size(); i++){    for (j = 0; j < list2.size(); j++) {        if(list1.get(i).equals(list2.get(j))){            comm.add(list1.get(i));            continue a;        }    }    if(j==list2.size()){        addmap.add(list1.get(i));    }}System.out.println(addmap);System.out.println(comm);

0 0
原创粉丝点击