zoukankan      html  css  js  c++  java
  • guava常用集合交集,差集,并集,补集操作


    <!-- 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

    guava-21.0.jar 
    javacsv.jar  

  • 相关阅读:
    Django基础六之ORM中的锁和事务
    Django学习之model进阶
    Django基础八之cookie和session
    Django基础五之django模型层(二)多表操作
    Django基础五之django模型层(一)单表操作
    Django基础四之模板系统
    webpack最佳入门实践系列(4)
    webpack最佳入门实践系列(3)
    webpack最佳入门实践系列(2)
    webpack最佳入门实践系列(1)
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/11841623.html
Copyright © 2011-2022 走看看