【Sets】使用Google Guava工程中Sets工具包,实现集合的并集/交集/补集/差集
来源:互联网 发布:淘宝卖家软件工具吧 编辑:程序博客网 时间:2024/05/29 03:54
获取两个txt文档的内容~存储进集合中求集合的并集/交集/补集/差集
package com.sxd.readLines.aboutDB;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.HashSet;import java.util.Set;import com.google.common.collect.Sets;public class Test { /** * * @throws IOException */ @org.junit.Test public void test1() throws IOException { Set<String> set1 = readFile4List(new File("D:/B/1.txt")); Set<String> set2 = readFile4List(new File("D:/B/DB.txt")); Set<String> result1 = Sets.union(set1, set2);//合集,并集 Set<String> result2 = Sets.intersection(set1, set2);//交集 Set<String> result3 = Sets.difference(set1, set2);//差集 1中有而2中没有的 Set<String> result4 = Sets.symmetricDifference(set1, set2);//相对差集 1中有2中没有 2中有1中没有的 取出来做结果 //可以分别把4种不同结果 写出文件 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File("d:/B/result.txt"))); bufferedWriter.write("共有:"+result1.size()+"条\r\n"); for (String string : result1) { bufferedWriter.write(string+"\r\n"); } bufferedWriter.close(); } public Set<String> readFile4List(File file) throws IOException{ BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); Set<String> set = new HashSet<String>(); String str = null; while((str =bufferedReader.readLine()) != null){ if(str.length() > 6){ set.add(str.substring(3)); }else{ set.add(str); } } return set; } }
阅读全文
0 0
- 【Sets】使用Google Guava工程中Sets工具包,实现集合的并集/交集/补集/差集
- java guava 集合的操作:交集、差集、并集
- java求集合的并集、交集、补集、差集和对称差集
- 两个集合的交集,并集,差
- js 集合的交集、补集、差集、去重
- 集合中的(交集,并集,差集,补集,对称差集)
- 集合求取并集交集补集
- list集合交集 并集 补集
- shell中使用sort和uniq实现两文件的并集,交集和差集
- python求补集合、交集、并集
- javaSet集合的并集、差集、交集
- 计算集合的并集、交集、差集
- 求List集合的并集、交集、差集
- List集合的并集、交集、差集以及源码
- 求两个集合的交集、差集和并集
- JavaScript Set集合的并集,交集,差集
- java集合的交集,并集,差集
- Google Code Jam2008 Round1B Number Sets(并查集)
- 【Redis】复制、sentinel与集群
- 2017 计蒜之道 第二场 A. 百度的年会游戏
- ios 程序在内存中运行时,内存分几个区,各自用途?
- Tomcat部署项目去掉项目名称
- app 证书和上架流程
- 【Sets】使用Google Guava工程中Sets工具包,实现集合的并集/交集/补集/差集
- SQL 常用增删改查栗子
- linux 内存占用情况查看并优化
- 求解定积分的并行算法——基于MPI
- 用xampp本地创建的网站升级wordpress时提示另一更新正在进行。
- 前端面试大全(三)
- c语言基础及坑点
- idea常用快捷键
- PCB的敷铜问题