importcom.csvreader.CsvReader;importcom.google.common.collect.Sets;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.io.IOException;importjava.nio.charset.Charset;import java.util.*;import staticcom.google.common.collect.Sets.newHashSet;/*** @Author: SimonHu
* @Date: 2019/11/12 14:35
* @Description:*/
public classBillCompareUtil {private static final Logger log = LoggerFactory.getLogger(BillCompareUtil.class);public static voidmain(String[] args) {
String csvFilePath02= "C:\\Users\\admin\\Desktop\\app订单0905-02.csv";
String csvFilePath01= "C:\\Users\\admin\\Desktop\\app订单0905.csv";
compare(csvFilePath01, csvFilePath02,3);
}/***@parampath1
*@parampath2
*@paramtype 1交集2差集3并集
*@returnjava.util.Map
* @Description:对比文档 运行出错,请注意使用jdk1.8以上进行编译
* @Author:SimonHu
* @Date: 2019/11/12 14:44*/
public static Map compare(String path1, String path2, inttype) {
ArrayList csvList01 = new ArrayList();
ArrayList csvList02 = new ArrayList();try{long start =System.currentTimeMillis();
Set> set1 =newHashSet();
Set> set2 =newHashSet();//解决中文编码
CsvReader reader01 = new CsvReader(path1, ',', Charset.forName("GBK"));
CsvReader reader02= new CsvReader(path2, ',', Charset.forName("GBK"));//reader.readHeaders();//跳过表头 如果需要表头的话,不要写这句。//逐行读入除表头的数据
while(reader01.readRecord()) {
csvList01.add(reader01.getValues());
}//逐行读入除表头的数据
while(reader02.readRecord()) {
csvList02.add(reader02.getValues());
}
reader01.close();
reader02.close();for (int row = 0; row < csvList01.size(); row++) {
Map map01 = newHashMap();
String cell0= csvList01.get(row)[0];
String cell1= csvList01.get(row)[1];
map01.put("订单号", cell0);if (cell1.indexOf(".") == -1) {
map01.put("金额", cell1);
}else{
String cell2= cell1.substring(0, cell1.indexOf("."));
map01.put("金额", cell2);
}
set1.add(map01);
}for (int row = 0; row < csvList02.size(); row++) {
Map map02 = newHashMap();
String cell0= csvList02.get(row)[0];
String cell1= csvList02.get(row)[1];if (cell1.indexOf(".") == -1) {
map02.put("订单号", cell0);
map02.put("金额", cell1);
}else{
String s01= cell1.substring(0, cell1.indexOf("."));
map02.put("订单号", cell0);
map02.put("金额", s01);
}
set2.add(map02);
}
Map map= newHashMap();
List resultList = new LinkedList<>();
Sets.SetView result= null;if (type == 1) {
result=Sets.intersection(set1, set2);
System.out.println("交集 intersection:");//intersection交集:
} else if (type == 2) {
result=Sets.difference(set1, set2);
System.out.println("补集 difference:");//difference 补集:这里其实是set2相对于set1的补集
} else if (type == 3) {
result=Sets.union(set1, set2);
System.out.println("并集 union:");
}else if(type == 4){
//差集
result = Sets.symmetricDifference(set1,set2)