JAVA set operation to obtain data difference (complement)

The complement generally refers to the absolute complement, that is, in general, let S be a set, A is a proper subset of S, and the set composed of all elements in S that do not belong to A is called the absolute complement of subset A in S (referred to as complement or complement).

Complements in programs are generally used to compare data differences.
For example: cloud synchronization mobile phone contacts
mobile phone cloud
Zhang San 10010 Zhang San 10013
Li Si 10011 Wang Ba 10016
Wang Wu 10012 Li Si 10011
In the case of a large amount of data, we may need the program to filter out the difference data for data synchronization.
Relative complement

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Test {

   
    public static void main(String[] args) {
        // TODO Auto -generated method stub

   
          List> l_m1 = new ArrayList>();
              Map map1=new HashMap();
              map1.put("id", "1");
              map1.put("hash", "10086");
              Map map2=new HashMap();
              map2.put("id", "2");
              map2.put("hash", "10087");
              Map map3=new HashMap();
              map3.put("id", "3");
              map3.put("hash", "10088");
              Map map8=new HashMap();
              map8.put("id", "8");
              map8.put("hash", "88888");
              l_m1.add(map1);
              l_m1.add(map2);
              l_m1.add(map3);
              l_m1.add(map8);
          List> l_m2 = new ArrayList>();
              Map map4=new HashMap();
              map4.put("id", "4");
              map4.put("hash", "10089");
              Map map5=new HashMap();
              map5.put("id", "5");
              map5.put("hash", "10090");
              Map map6=new HashMap();
              map6.put("id", "6");
              map6.put("hash", "10091");       
              Map map9=new HashMap();
              map9.put("id", "8");
              map9.put("hash", "88888");
              l_m2.add(map4);
              l_m2.add(map5);
              l_m2.add(map6);         
              l_m2.add(map9);
              System.out.println(Test.retainAll(l_m1, l_m2).toString());;
    }
   
   
   
    public  static Map retainAll(List> l1,List> l2){
        List l1_h=new ArrayList();
        Map> l1_h_m=new HashMap>();
        List l2_h=new ArrayList();
        Map> l2_h_m=new HashMap>();
        for(int i=0;i
            l1_h.add(i, l1.get(i).toString().hashCode());
            l1_h_m.put(l1.get(i).toString().hashCode(),l1.get(i));
        }
        for(int i=0;i
            l2_h.add(i, l2.get(i).toString().hashCode());
            l2_h_m.put(l1.get(i).toString().hashCode(),l1.get(i));
        }
        l1_h.retainAll(l2_h);
       
        for(Integer hash:l1_h){
            l1_h_m.remove(hash);
        }
        for(Integer hash:l1_h){
            l2_h_m.remove(hash);
        }

        List> rl1=new ArrayList>();
        for(Map.Entry> entry  : l1_h_m.entrySet()) {
            rl1.add(entry.getValue());
        }   

        List> rl2=new ArrayList>();
        for(Map.Entry> entry  : l2_h_m.entrySet()) {
            rl2.add(entry.getValue());
        }           
        Map rsm=new HashMap();
        rsm.put("l1", rl1);
        rsm.put("l2", rl2);
        return rsm;
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326353052&siteId=291194637