<!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>21.0</version> </dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -android結尾兼容jdk1.7 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0-android</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv -->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
引用guava jar文件
import com.csvreader.CsvReader; import com.google.common.collect.Sets; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.nio.charset.Charset; import java.util.*; import static com.google.common.collect.Sets.newHashSet; /** * @Author: SimonHu * @Date: 2019/11/12 14:35 * @Description: */ public class BillCompareUtil { private static final Logger log = LoggerFactory.getLogger(BillCompareUtil.class); public static void main(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); } /** * @param path1 * @param path2 * @param type 1交集2差集3並集 * @return java.util.Map * @Description:對比文檔 運行出錯,請注意使用jdk1.8以上進行編譯 * @Author:SimonHu * @Date: 2019/11/12 14:44 */ public static Map compare(String path1, String path2, int type) { ArrayList<String[]> csvList01 = new ArrayList<String[]>(); ArrayList<String[]> csvList02 = new ArrayList<String[]>(); try { long start = System.currentTimeMillis(); Set<Map<String, String>> set1 = newHashSet(); Set<Map<String, String>> 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<String, String> map01 = new HashMap(); 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<String, String> map02 = new HashMap(); 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 = new HashMap(); List<Map> 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)
} int i=0; for (Object u : result) { i++; Map paramMap = new HashMap(); System.out.println(u.toString()); paramMap.put("text","序號:"+i+"------"+u.toString()); resultList.add(paramMap); } Map countMap = new HashMap(); countMap.put("text","總計:"+i+" 條"); resultList.add(0,countMap); map.put("list", resultList); long end = System.currentTimeMillis(); System.out.println((end - start) + "==========ms"); return map; } catch (IOException e) { log.error("文件對比失敗------", e); return null; } } }
csv源文件
app訂單0905-02.csv
"201992307383892601","490000.00" "201950907854593602","490000.00"
app訂單0905.cdv
"201992307383892601","490001.00" "201950907854593602","490000.00"
對比結果
交集 intersection:
{orderNo=201950907854593602, trxAmt=490000}
補集 difference:
{orderNo=201992307383892601, trxAmt=490001}
並集 union:
{orderNo=201992307383892601, trxAmt=490001}
{orderNo=201950907854593602, trxAmt=490000}
{orderNo=201992307383892601, trxAmt=490000}
差集 symmetricDifference:
{orderNo=201950907854593602, trxAmt=4901}
{orderNo=201992307383892601, trxAmt=4900}
{orderNo=201950907854593602, trxAmt=4902}
lib引用jar

