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 获取两个列表的交集并集.补集###### ...
随机推荐
- selenium网页截图和截图定位(无界面)phantomjs
phantomjs是一款软件,需要重新安装. 参考: https://blog.csdn.net/liyahui_3163/article/details/79064108 案例代码: from se ...
- python Beautiful Soup 采集it books pdf,免费下载
http://www.allitebooks.org/ 是我见过最良心的网站,所有书籍免费下载 周末无聊,尝试采集此站所有Pdf书籍. 采用技术 python3.5 Beautiful soup 分享 ...
- Image Processing and Analysis_15_Image Registration:HAIRIS: A Method for Automatic Image Registration Through Histogram-Based Image Segmentation——2011
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- 详解分布式系统里session同步
1.什么是session?什么又是cookie?他俩有啥联系和区别? 2.为什么要在多台服务器间进行session的共享同步? 3.以及有哪些方法来实现这个同步? 大家快搬板凳,老王开始扯淡咯~ 1. ...
- 中国大学MOOC课程信息之数据分析可视化二
版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82318571 - 写在前面 本篇博客继续对中国大学MOOC ...
- idou老师教你学Istio06: 如何用istio实现流量迁移
流量迁移是流量管理的一个重要功能.istio提供的流量管理功能将流量从基础设施扩展中解耦,支持动态请求路由,故障注入.超时重试.熔断和流量迁移等.流量迁移的主要目的是将流量从微服务的某一版本的逐步迁移 ...
- Git学习笔记08-远程仓库
因为想在家里和公司都能用到一套代码,所以选择上传到github,记录一下使用经验. 需要安装git,和注册github 以下操作是第一次将自己的代码上传到GitHub上 1)创建github项目 1. ...
- PL/SQL查询,字段名添加中文别名,查询结果的字段名会显示问号,处理方法:
一开始查询出来的字段名显示的是???,下面说说解决方法(本人也是在网上看到的,算是重复编辑一下): -------------------------------------------------- ...
- CentOS7主机SSH连接失败
说来话长,之前20刀一年买bandwagon的廉价VPS,由于做了一些违法的事情,导致ip被封了. 检测ip被封的方法:进入ping.chinaz.com:输入IP地址,如果国外节点能够Ping通而国 ...
- c语言1博客作业10
一.本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/SE2019-3/homework/10101 ...