guava常用集合操作

 <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
引用guava jar文件
package zhx.com;

import com.csvreader.CsvReader;
import com.google.common.collect.Sets;

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 13:31
 * @Description:
 */
public class GuavaTest {
    static ArrayList<String[]> csvList = new ArrayList<String[]>(); //用来保存数据
    static ArrayList<String[]> csvList02 = new ArrayList<String[]>(); //用来保存数据
    public static void main(String[] args) throws IOException {
        sort();
//        setMethod();
    }
    
    /**
     * @return void
     * @Description:set方法
     * @Author:SimonHu
     * @Date: 2019/10/22 11:01
     */
    public static void setMethod() {
        HashSet setA = newHashSet(1, 2, 3, 4, 5);
        HashSet setB = newHashSet(4, 5, 6, 7, 8);
        Sets.SetView union = Sets.union(setA, setB);
        System.out.println("union:");
        //union 并集:12345867
        for (Object integer : union) {
            System.out.println(integer);
        }
        Sets.SetView difference = Sets.difference(setA, setB);
        System.out.println("difference:");
        //difference 差集:123
        for (Object integer : difference) {
            System.out.println(integer);
        }
        Sets.SetView intersection = Sets.intersection(setA, setB);
        System.out.println("intersection:");
        //intersection 交集:45
        for (Object integer : intersection) {
            System.out.println(integer);
        }
    }
    
    public static void sort() throws IOException {
        Set<Map<String, String>> set = newHashSet();
        Set<Map<String, String>> set2 = newHashSet();
        //注明:或者可以通过前端上传的文件,用一个方法获取上传文件名uploadName(String)
        String csvFilePath02 = "C:\\Users\\admin\\Desktop\\app订单0905-02.csv";
        String csvFilePath01 = "C:\\Users\\admin\\Desktop\\app订单0905.csv";
        CsvReader reader = new CsvReader(csvFilePath01, ',', Charset.forName("GBK"));    //解决中文编码
        CsvReader reader02 = new CsvReader(csvFilePath02, ',', Charset.forName("GBK"));
//        reader.readHeaders(); // 跳过表头   如果需要表头的话,不要写这句。
        while (reader.readRecord()) { //逐行读入除表头的数据
            csvList.add(reader.getValues());
        }
        while (reader02.readRecord()) { //逐行读入除表头的数据
            csvList02.add(reader02.getValues());
        }
        reader.close();
        reader02.close();
        for (int row = 0; row < csvList.size(); row++) {
            Map<String, String> map01 = new HashMap();
            String cell0 = csvList.get(row)[0];
            String cell1 = csvList.get(row)[1];
            map01.put("orderNo", cell0);
            if (cell1.indexOf(".") == -1) {
                map01.put("trxAmt", cell1);
            } else {
                String cell2 = cell1.substring(0, cell1.indexOf("."));
                map01.put("trxAmt", cell2);
            }
            set.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("orderNo", cell0);
                map02.put("trxAmt", cell1);
            } else {
                String s01 = cell1.substring(0, cell1.indexOf("."));
                map02.put("orderNo", cell0);
                map02.put("trxAmt", s01);
            }
            set2.add(map02);
        }
        
        
        Sets.SetView intersection = Sets.intersection(set, set2);
        System.out.println("交集  intersection:");
        //intersection交集:
        for (Object u : intersection) {
            System.out.println(u.toString());
        }
        Sets.SetView difference = Sets.difference(set, set2);
        System.out.println("左差集  difference:");
        //difference 差集:
        for (Object dif : difference) {
            System.out.println(dif.toString());
        }
        Sets.SetView union = Sets.union(set, set2);
        System.out.println("并集 union:");
        //union 并集:
        for (Object u : union) {
            System.out.println(u.toString());
        }
    }
}

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}

lib引用jar

guava-21.0.jar 
javacsv.jar  

猜你喜欢

转载自www.cnblogs.com/SimonHu1993/p/11841623.html