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 获取两个列表的交集并集.补集###### ...
随机推荐
- Genymotion上运行ARM架构Android项目
问题 Genymotion是x86的模拟器,在集成一些第三方的SDK时需要添加一些放在armeabi.armeabi-64文件夹下面的.so文件,这些文件在arm架构下才能运行.这时模拟器就不能用了. ...
- 【ASE模型组】Hint::neural 模型与case study
模型 基于搜索的提示系统 我们的系统用Pycee针对语法错误给出提示.然而,对于语法正确.结果错误的代码,我们需要另外的解决方式.因此,我们维护一些 (错误代码, 相应提示) 的数据,该数据可以由我们 ...
- orecle 查询数量 union合并 的排序问题
orecle 查询数量 union合并 之后按照从小到大排序了,这边需要不排序的 直接将union 改成union all 就解决了. 图就不传了,验证没问题
- Sharing is only supported for boot loader classes because bootstrap classpath has been appended
在idea里面运行项目,terminal里面报“Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boo ...
- selenium 操作多个窗口间切换
#coding=gbk ''' selenium多个窗口间切换 ''' from selenium import webdriver as wd from selenium.webdriver imp ...
- java之rpc/orm
Netty线程模型 其中ChannelPiepline的设计模型采用的是Handler组成的责任链模型 blocking I/O 阻塞nonblocking I/O 非阻塞I/O multiplexi ...
- MySql忘记密码了咋办
对内 忘记密码终端修改操作: #停止mysql服务 sudo /opt/lampp/lampp stopmysql #参数启动mysqld sudo /opt/lampp/sbin/mysqld -- ...
- JS基本数据类型和引用数据类型区别
1.栈(stack)和堆(heap) stack为自动分配的内存空间,它由系统自动释放:而heap则是动态分配的内存,大小也不一定会自动释放 2.数据类型 JS分两种数据类型: 基本数据类型:Numb ...
- Web缓存Cache、Application、Session与ViewState
在ASP.NET中,有很多种保存信息的对象.例如:APPlication,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么? 方法 信息 ...
- 如何DIY个性PE
前言:有时候在网络上能找到很不错的PE(无忧启动论坛),但是有时候PE的功能仍不能满足自己的需要(软件过旧,缺少某些功能),这时候就显得自己DIYPE的重要性 需要的工具: WIMTOOL(必备) 软 ...