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常用集合交集,差集,并集,补集操作的更多相关文章
- python-->(set /dict)交集 差集 并集 补集(功能用来做交差并补的)
# ### 集合 作用:交集 差集 并集 补集(功能用来做交差并补的) '''特征:自动去重 无序''' #定义一个空集合 setvar = set() #set()强制转换成一个空集合的数据类型 p ...
- js取两个数组的交集|差集|并集|补集|去重示例代码
http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...
- C# List 集合 交集、并集、差集、去重, 对象集合、 对象、引用类型、交并差补、List<T>
关键词:C# List 集合 交集.并集.差集.去重, 对象集合. 对象.引用类型.交并差.List<T> 有时候看官网文档是最高效的学习方式! 一.简单集合 Intersect 交集, ...
- C# 集合的交集 差集 并集 去重
C# 集合的交集 差集 并集 去重 两个对象list,直接比较是不行的,因为他们存的地址不一样 需要重写GetHashCode()与Equals(object obj)方法告诉电脑 class Stu ...
- NET 集合交集、并集、差集操作
, , , , , , , }; , , , , , , , , }; // List1:1 3 5 7 9 11 13 15 Console.WriteLine("List1:" ...
- 【python】集合 list差集|并集|交集
两个list差集 list(set(b).difference(set(a))) # b中有而a中没有的 示例: a=[1,2,3] b=[2,3] list(set(a).difference(se ...
- C# 数组比较--取得两个集合的交集,差集,并集的方法
方法关键字: 交集:Intersect 差集:Except 并集:Union 使用代码: , , , , }; , , , , }; var 交集 = arr1.Intersect(arr2).ToL ...
- java数组并集/交集/差集(补集)
1.说明 使用java容器类的性质选择容器 2.实现 package com.wish.datastrustudy; import java.util.HashSet; import java.uti ...
- grep和map计算两个集合交集、并集、补集
#!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集###### ...
随机推荐
- ajax检查用户名重复
1.获取ajax对象 new XMLHttpRequest(); IE6-8: new ActiveXOject("Microsoft.XMLHTTP"); 兼容判断:if(XML ...
- ES6对数组的增强
来看数组的改变,Array.from()可以将类数组对象变为数组: Array.of方法用于将一组值,转化为数组: 寻找数组中是否拥有某项find().findIndex(),里面要放置回调函数: 要 ...
- div上中下布局中间自适应
需求1: 头尾固定高度,中间自适应 1.上部(header)Div高度固定100px,宽度100%: 2.下部(footer)Div高度固定100px,宽度100%: 3.中部(middle)DIV高 ...
- centos7安装google浏览器
1. 配置yum源 在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo cd /ect/yum.repos.d/ vim google-chrome.repo ...
- Java 面向对象(五)抽象
一.抽象概述 1.由来 父类中的方法,被它的子类们重写,子类各自的实现都不尽相同.那么父类的方法声明和方法主体,只有声明还有意义,而方法主体则没有存在的意义了. 我们把没有方法主体的方法称为抽象方法. ...
- 一段代码显示出电脑连过所有wifi的密码
1.打开运行 2.输入cmd后回车 3.输入如下代码 for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show pro ...
- Image Processing and Analysis_15_Image Registration: A Method for Registration of 3-D shapes——1992
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
在settings.py中增加 INSTALLED_APPS = [ ... 'django.contrib.sites', ] 问题就解决了.什么原因.——不知道.. 具体请看: https://s ...
- webpack中配置eslint
首先安装eslint npm install eslint --save-dev 安装好这个工具后,初始化eslint npx eslint --init 这个时候会自动生成.eslintrc.js ...
- 深度解析Droupout与Batch Normalization
Droupout与Batch Normalization都是深度学习常用且基础的训练技巧了.本文将从理论和实践两个角度分布其特点和细节. Droupout 2012年,Hinton在其论文中提出Dro ...