Statistic the intersection of two sets of data and complement (the difference comparison algorithm of old and new data) traverse once

Source: Internet
Author: User
Tags addall comparable diff


Old Data A = {}
New data B = {}

Added: B-a = {x | x∈b and X∉a}
Delete entry: A-b = {x | x∈a and X∉b}
Total items: b∩a = {x | x∈b and X∈a}

ImportJava.io.BufferedReader;Importjava.io.Closeable;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.Iterator;Importjava.util.List; Public classRecorddiff { Public Static voidMain (string[] args) {//statistics on changes to old data based on new data: New and old items from the original key field of the database constrained to the self-growing integer, that is, not repeating integers, sorted in ascending orderinteger[] as=NewInteger[] {1, 2, 3, 8, 9, 12 }; Integer[] BS=NewInteger[] {1, 2, 3, 9, 10, 12, 18, 22 }; //bs = new integer[] {1, 2, 3, 9};SYSTEM.OUT.PRINTLN ("Old item:" +arrays.tostring (AS)); System.out.println ("New Item:" +arrays.tostring (BS)); System.out.println ("===================="); List<Integer> alist = Arrays.aslist (AS);//Old Datalist<integer> blist = Arrays.aslist (BS);//New DataDiff<Integer> diff1 =diff.difference (alist, blist); System.out.println ("All have items:" +arrays.tostring (Diff1.unionList.toArray ())); System.out.println ("NEW:" +arrays.tostring (Diff1.addedList.toArray ())); System.out.println ("Delete Item:" +arrays.tostring (Diff1.removedList.toArray ())); System.out.println ("===================="); List<Integer> fromonlylist =NewArraylist<integer>(); List<Integer> againstonlylist =NewArraylist<integer>(); List<Integer> bothlist =NewArraylist<integer>();        Diff.diff (Alist, blist, Fromonlylist, Againstonlylist, bothlist); System.out.println ("All have items:" +arrays.tostring (Bothlist.toarray ())); System.out.println ("NEW:" +arrays.tostring (Againstonlylist.toarray ())); System.out.println ("Delete Item:" +arrays.tostring (Fromonlylist.toarray ())); }    /*** Compare the two sets of data to get the old and new differences: There are items, additions, deletions * *@authorFANGSS * *@param<T>*/     Public Static classDiff<textendsComparable<t>> {        /**Total Items*/List<T>unionlist; /**Difference Item*/List<T>Addedlist, Removedlist; /**         *          * @paramUnionlist * Acceptance has a result of the item buffer *@paramAddedlist * Accept the result of the new item buffer *@paramRemovedlist * Accept the result of the deleted item buffer*/         PublicDiff (list<t> unionlist, list<t> addedlist, list<t>removedlist) {            Super();  This. unionlist =unionlist;  This. addedlist =addedlist;  This. removedlist =removedlist; }        /*** Old and new data list two are only traversed once, applicable to data can only scroll forward once, such as read the file line <br> * A B 6 1 7 \ 2 8 \ 3 9 \ 5 10.6 11.10 12 16 17 1 8 * *@paramFromList * must be ordered, and ascending, usually old data, the List to compare from *@paramAgainstlist * must be ordered and ascending, generally new data a List to compare against *@paramfromonlylist * Supplement Set *@paramagainstonlylist * Supplement Set *@paramBothlist * Intersection*/         Public Static<textendsComparable<t>>voiddiff (List<t> FromList, list<t> againstlist, list<t>fromonlylist, List<T> Againstonlylist, list<t>bothlist) {            //0-both, ' f '-from, ' a '-against, and the comparison result is the same: the same size is moved, otherwise who small who moves            intWhomakeway = ' B '; Iterator<T> Fromiterator =Fromlist.iterator (); Iterator<T> Againstiterator =Againstlist.iterator (); T from=NULL, against =NULL;  while(true) {T Fromnext=NULL; if(' a '! =Whomakeway) {                    if(Hasnextorexhaustrival (Fromiterator,NULL, Againstiterator, againstonlylist)) { from=Fromiterator.next (); Fromnext=From ; } Else {                        return; }                }                if(' f '! =Whomakeway) {                    if(Hasnextorexhaustrival (Againstiterator, Fromnext, Fromiterator, fromonlylist)) {against=Againstiterator.next (); } Else {                        return; }                }                //The first two are judged to have the next, and then move, or first move there is the next and the other No, the former one only own the loss of a                intCmpresult =From.compareto (against); //Those who move small, the same move.                 if(0 = =Cmpresult) {Whomakeway= ' B ';                Bothlist.add (from); } Else if(0 >Cmpresult) {                    //From < Against:fromiterator continue until flat 0 or more than 1Whomakeway = ' F ';                Fromonlylist.add (from); } Else {                    //From > against:againstiterator continue until flat 0 or more than 1Whomakeway = ' a ';                Againstonlylist.add (against); }            }        }         Public Static<textendsComparable<t>>BooleanHasnextorexhaustrival (iterator<t>Hasnext, T rivalcurval, Iterator<T> rival, list<t>list) {            if(Hasnext.hasnext ()) {return true; }            if(NULL!=rivalcurval)            {List.add (rivalcurval); }             while(Rival.hasnext ()) {List.add (Rival.next ()); }            return false; }        /*** Old and new data list two traversal may be more than once * *@paramNewList * must be ordered, and ascending *@paramOldlist * must be ordered, and ascending *@paramUnionlist *@paramAddedlist *@paramremovedlist*/        Private Static<T>voidInnerdifference (list<t> newlist, list<t> oldlist, list<t> unionlist, List<T>addedlist, List<T>removedlist) {             for(Iterator<t> Iterator =removedlist.iterator (); Iterator.hasnext ();) {T Item=Iterator.next (); if(Addedlist.contains (item)) {Unionlist.add (item);                    Iterator.remove ();                Addedlist.remove (item); }            }        }        /*** Old and new data list two traversal may be more than once * *@paramnewlist * New data, must be ordered, and ascending *@paramoldlist * Old data, must be ordered, and ascending *@return         */         Public Static<textendscomparable<t>> diff<t> Difference (list<t> newlist, list<t>oldlist) {List<T> unionlist =NewArraylist<t>(); List<T> addedlist =NewArraylist<t>(oldlist); List<T> removedlist =NewArraylist<t>(NewList);            Innerdifference (NewList, Oldlist, Unionlist, Addedlist, removedlist); return NewDiff<t>(Unionlist, Addedlist, removedlist); }        /*** Old and new data list two traversal may be more than once * *@paramcursorlist * New data, must be ordered, and ascending *@parambaselist * Old data, must be ordered, and ascending *@paramUnionlist *@paramAddedlist *@paramremovedlist*/         Public Static<textendsComparable<t>>voidDifference (list<t> cursorlist, list<t> baselist, list<t>unionlist, List<T> Addedlist, list<t>removedlist)            {Addedlist.addall (cursorlist);            Removedlist.addall (baselist);        Innerdifference (Cursorlist, Baselist, Unionlist, Addedlist, removedlist); }    }     PublicList diff (String afilepath, String Bfilepath, String resultfilepath)throwsfilenotfoundexception {BufferedReader Areader=NULL, Breader =NULL;        String ALine, BLine; String delimiter= " "; Try{Areader=NewBufferedReader (NewFileReader (Afilepath)); Breader=NewBufferedReader (NewFileReader (Afilepath)); if(NULL! = (ALine =Areader.readline ())) {            }        } Catch(Exception e) {//Todo:handle Exception}finally{closequietly (areader);            closequietly (Breader);        closequietly (Areader); }        return NULL; }     Public Static<textendsCloseable>T closequietly (t C) {if(NULL!=c) {Try{c.close (); } Catch(IOException e) {//alog.d ("Close", E.getmessage ());            }        }        return NULL; }}

Statistic the intersection of two sets of data and complement (the difference comparison algorithm of old and new data) traverse once

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.