Require
/** * Before: * file a1.csv {1,2,3,4,5} * file A2.csv {2,3,9,10,11} * file B1.csv {5,12,13,14,15} * file B2.csv {16,14,15, 4,9,20,30} * a1.csv a2.csv a3.csv a4.csv cant not repeat * * After: * file a1.csv {1,4} * file A2.csv {2,3,10,11} * file B 1.csv {12,13} * file B2.csv {16,9,20,30} */
[Email protected]:~/csvrepeat$ lsa1.csv a2.csv b1.csv b2.csv
Csvutilversion2.java
Import Lombok.extern.slf4j.slf4j;import Org.apache.commons.collections.collectionutils;import Org.apache.commons.io.fileutils;import Org.apache.commons.io.lineiterator;import Org.springframework.util.stringutils;import java.io.*;import Java.lang.reflect.array;import java.util.*;/** * Before: * file a1.csv {1,2,3,4,5} * file A2.csv {2,3,9,10,11} * file B1.csv {5,12,13,14,15} * file B2.csv {16,14,15,4,9,20 A1.csv a2.csv a3.csv a4.csv cant not repeat * * After: * file a1.csv {1,4} * file A2.csv {2,3,10,11} * file B1.csv {12,13} * file B2.csv {16,9,20,30} */@Slf4jpublic class CSVUtilVersion2 {private static final String Csv_path = "/home /tangxin/csvrepeat/"; Private static Final Boolean create_switch = true; /** * Read single column data list * @param path * @return */public static list<string> IDs (Stri ng path) {list<string> result = new arraylist<> (); File CSV = new file (path); CSV file path Lineiterator it = null; try {it = fileutils.lineiterator (CSV); while (It.hasnext ()) {String line = It.nextline (); if (Line.trim (). Contains ("ID")) {continue; } string[] arr = Line.split (","); String ID = arr[0]; ID = Id.replaceall ("\" "," "). Trim (); if (! Stringutils.isempty (ID)) {result.add (ID); }}} catch (Exception e) {log.error ("Read ID CSV file failed: {}", E.getmessage ()); } finally {lineiterator.closequietly (it); } return result; }/** * from src delete oth * @param src * @param oth * @return */public static List RemoveAll ( List src, list oth) {LinkedList result = new LinkedList (SRC); HashSet Othhash = new HashSet (OTH); Iterator iter = Result.iterator (); while (Iter.hasnext ()) {if (othhash.contains(Iter.next ())) {Iter.remove (); }} return result; }/** *-XMS1G-XMX1G-XX:PERMSIZE=128M-XX:SURVIVORRATIO=2-XX:+USEPARALLELGC * @param args * @throws exce Ption */public static void main (string[] args) throws Exception {//∑=1+2+3+...+ (n-1) group Linkedli st<string> fileList = new linkedlist<> (); Filelist.add ("A1.csv"); Filelist.add ("A2.csv"); Filelist.add ("B1.csv"); Filelist.add ("B2.csv");//Filelist.add ("C1.csv"); Descartesrepeat (fileList); Ded (fileList); } private static void Descartesrepeat (Linkedlist<string> fileList) {set<string> repeatlist = new Ha Shset<> (); set<string> groupset = new hashset<> (); set<string> goonlist = new hashset<> (); A1->A2,B1,B2 for (int i = 0; i < filelist.size (); i++) {String Itemi = Filelist.get (i); FoR (Int j = 0; J < Filelist.size (); j + +) {String ITEMJ = Filelist.get (j); if (!itemi.equals (ITEMJ)) {String groupR1 = Itemi + "+" + itemj; String groupR2 = itemj + "+" + Itemi; if (Groupset.contains (groupR1) | | groupset.contains (GROUPR2)) {continue; } groupset.add (groupR1); String Repeatt = repeat (Csv_path + itemi, Csv_path + ITEMJ); if (! Stringutils.isempty (Repeatt)) {Repeatlist.add (Repeatt); System.out.println (groupr1+ "-" +repeatt); }}}} if (Collectionutils.isnotempty (repeatlist)) {//System.out.prin TLN (repeatlist); for (String repeatitem:repeatlist) {iterator<string> Iterator = Filelist.iterator (); while (ITERATOR.HASNExt ()) {String Olditem = Iterator.next (); String OldS = Olditem.replace (". csv", ""). Replace ("-new", "" "); String RepeatS = Repeatitem.replace (". csv", ""). Replace ("-new", "" "); if (Repeats.contains (OldS)) {iterator.remove (); Goonlist.add (Repeatitem); }}} filelist.addall (Goonlist); System.out.println (fileList); Descartesrepeat (fileList); }} public static void Ded (List<string> args) {//ensures that the specified CSV list cannot have duplicate data for (int i = 0; i < arg S.size (); i++) {//if (i>0) {//continue;//} String Source = Csv_path + Args.get (i ); for (int j = 0; J < Args.size (); j + +) {if (i = = j) {continue; } String target = Csv_path + Args.get (j);Intersection (source, target); }}} public static void intersection (String sourcepath, String TargetPath) {list<string> ids1 = IDs (SourcePath); list<string> ids2 = IDs (TargetPath); list<string> inter = (list<string>) collectionutils.intersection (IDS1, IDS2); System.out.println (SourcePath + "and" + TargetPath + "for repeat data size + inter.size ()); public static string repeat (string source, string target) {//CDD fund xyd list<string> ids1 = IDs (source); list<string> ids2 = IDs (target),//System.out.println (source + "collection size" + ids1.size ());//System.out.pri NTLN (target + "set size" + ids2.size ()); list<string> inter = (list<string>) collectionutils.intersection (IDS1, IDS2);//System.out.println ("de-weight According to size: "+ inter.size ()); if (inter! = null && inter.size () > 0) {if (Ids1.size () > Ids2.size ()) {return R EpeaTinner (source, ids1, Inter); } else if (Ids2.size () > Ids1.size ()) {return Repeatinner (target, ids2, Inter); } else {return Repeatinner (source, ids1, Inter); }} return ""; } private static string Repeatinner (string source, list<string> IDs, list<string> inter) {string NE WPath = Source.replace (". csv", "-new.csv"); list<string> ids1new = RemoveAll (IDs, Inter); Createcsv (Ids1new, NewPath); Return Newpath.replace (Csv_path, ""); }/** * Create CSV file */public static void Createcsv (List<string> List, String fileName) {if (! Create_switch) {//SYSTEM.OUT.PRINTLN ("Create CSV switch off"); Return }else{//System.out.println ("Create CSV switch on"); }//table header object[] head = {"ID"}; list<object> headlist = arrays.aslist (head); Data list<list<object>> dataList = new Arraylist<> (); List<object> rowlist = null; for (int i = 0; i < list.size (); i++) {rowlist = new arraylist<> (); Rowlist.add (List.get (i)); Datalist.add (rowlist); } File CSVFile; BufferedWriter csvwtriter = null; try {csvfile = new File (fileName); File parent = Csvfile.getparentfile (); if (parent! = NULL &&!parent.exists ()) {parent.mkdirs (); } csvfile.createnewfile (); GB2312 the delimiter "," Csvwtriter = new BufferedWriter (new OutputStreamWriter (FileOutputStream), "CSVFile 312 "), 1024); Write file header Writerow (headlist, Csvwtriter); Write file contents for (list<object> row:datalist) {writerow (row, csvwtriter); } csvwtriter.flush (); } catch (Exception e) {e.printstacktrace (); } finally { try {csvwtriter.close (); } catch (IOException e) {e.printstacktrace (); }}}/** * Write a row of data * * @param row Data list * @param csvwriter * @throws IOException */private static void Writerow (list<object> row, BufferedWriter csvwriter) throws IOException {for (obje CT data:row) {stringbuffer sb = new StringBuffer (); String rowstr = sb.append ("\" "). Append (data). Append (" \ ","). ToString (); Csvwriter.write (ROWSTR); } csvwriter.newline (); }}
[Email protected]:~/csvrepeat$ lsa1.csv a1-new.csv a1-new-new.csv a2.csv a2-new.csv b1.csv B2.csv B2-new.csv B2-new-new.csv
java csv list cant not repeat