【Sets】使用Google Guava工程中Sets工具包,实现集合的并集/交集/补集/差集
获取两个txt文档的内容~存储进集合中求集合的并集/交集/补集/差集
交集视图处理
Set<String> aSet = new HashSet(); Set<String> bSet = new HashSet(); Set<String> cSet = new HashSet(); Sets.SetView<String> intersection = Sets.intersection(aSet, bSet); for (String s : intersection) { cSet.add(s); } //cSet就是最终交集结果Set
并集/交集/补集/差集/相对差集 的 基本代码
1 package com.sxd.readLines.aboutDB; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.util.HashSet; 10 import java.util.Set; 11 12 import com.google.common.collect.Sets; 13 14 public class Test { 15 16 /** 17 * 18 * @throws IOException 19 */ 20 21 @org.junit.Test 22 public void test1() throws IOException { 23 Set<String> set1 = readFile4List(new File("D:/B/1.txt")); 24 Set<String> set2 = readFile4List(new File("D:/B/DB.txt")); 25 26 Set<String> result1 = Sets.union(set1, set2);//合集,并集 27 Set<String> result2 = Sets.intersection(set1, set2);//交集 28 Set<String> result3 = Sets.difference(set1, set2);//差集 1中有而2中没有的 29 Set<String> result4 = Sets.symmetricDifference(set1, set2);//相对差集 1中有2中没有 2中有1中没有的 取出来做结果 30 31 //可以分别把4种不同结果 写出文件 32 33 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File("d:/B/result.txt"))); 34 bufferedWriter.write("共有:"+result1.size()+"条\r\n"); 35 for (String string : result1) { 36 bufferedWriter.write(string+"\r\n"); 37 } 38 bufferedWriter.close(); 39 40 41 } 42 43 public Set<String> readFile4List(File file) throws IOException{ 44 BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); 45 Set<String> set = new HashSet<String>(); 46 String str = null; 47 while((str =bufferedReader.readLine()) != null){ 48 if(str.length() > 6){ 49 set.add(str.substring(3)); 50 }else{ 51 set.add(str); 52 } 53 54 } 55 return set; 56 } 57 58 59 }
并集/交集/补集/差集/相对差集 的 静态工具类代码
import com.google.common.collect.Sets; import org.apache.commons.collections.CollectionUtils; import java.util.*; import java.util.stream.Collectors; /** * 集合运算工具集 * * @author xudong.shen * @date 2021/12/07 */ public class CollectionOperationUtil { /** * 合集,并集 * * @param t t * @param list list * @return {@link List}<{@link T}> */ public static <T> List<T> union(T t, List<T> list){ return union(Arrays.asList(t), list); } /** * 交集 * * @param t t * @param list list * @return {@link List}<{@link T}> */ public static <T> List<T> intersection(T t, List<T> list){ return intersection(Arrays.asList(t), list); } /** * 差集 1中有而2中没有的 * * @param list list * @param t t * @return {@link Set}<{@link T}> */ public static <T> List<T> difference(List<T> list, T t){ return difference(list, Arrays.asList(t)); } /** * 相对差集 1中有2中没有 2中有1中没有的 取出来做结果 * * @param list list * @param t t * @return {@link List}<{@link T}> */ public static <T> List<T> symmetricDifference(List<T> list, T t){ return symmetricDifference(list, Arrays.asList(t)); } /** * 合集,并集 * * @param list1 list1 * @param list2 list2 * @return {@link List}<{@link T}> */ public static <T> List<T> union(List<T> list1, List<T> list2){ if (CollectionUtils.isEmpty(list1) || CollectionUtils.isEmpty(list2)) return Collections.EMPTY_LIST; Set<T> union = union(new HashSet<>(list1), new HashSet<>(list2)); return union.stream().collect(Collectors.toList()); } /** * 交集 * * @param list1 list1 * @param list2 list2 * @return {@link List}<{@link T}> */ public static <T> List<T> intersection(List<T> list1, List<T> list2){ if (CollectionUtils.isEmpty(list1) || CollectionUtils.isEmpty(list2)) return Collections.EMPTY_LIST; Set<T> intersection = intersection(new HashSet<>(list1), new HashSet<>(list2)); return intersection.stream().collect(Collectors.toList()); } /** * 差集 1中有而2中没有的 * * @param list1 list1 * @param list2 list2 * @return {@link Set}<{@link T}> */ public static <T> List<T> difference(List<T> list1, List<T> list2){ if (CollectionUtils.isEmpty(list1) || CollectionUtils.isEmpty(list2)) return Collections.EMPTY_LIST; Set<T> difference = difference(new HashSet<>(list1), new HashSet<>(list2)); return difference.stream().collect(Collectors.toList()); } /** * 相对差集 1中有2中没有 2中有1中没有的 取出来做结果 * * @param list1 list1 * @param list2 list2 * @return {@link List}<{@link T}> */ public static <T> List<T> symmetricDifference(List<T> list1, List<T> list2){ if (CollectionUtils.isEmpty(list1) || CollectionUtils.isEmpty(list2)) return Collections.EMPTY_LIST; Set<T> symmetricDifference = symmetricDifference(new HashSet<>(list1), new HashSet<>(list2)); return symmetricDifference.stream().collect(Collectors.toList()); } /** * 合集,并集 * * @param set1 set1 * @param set2 set2 * @return {@link Set}<{@link T}> */ public static <T> Set<T> union(Set<T> set1, Set<T> set2){ if (CollectionUtils.isEmpty(set1) && CollectionUtils.isEmpty(set2)) return Collections.EMPTY_SET; if (CollectionUtils.isEmpty(set1)) return set2; if (CollectionUtils.isEmpty(set2)) return set1; Sets.SetView<T> union = Sets.union(set1, set2); Set<T> result = union.stream().collect(Collectors.toSet()); return result; } /** * 交集 * * @param set1 set1 * @param set2 set2 * @return {@link Set}<{@link T}> */ public static <T> Set<T> intersection(Set<T> set1, Set<T> set2){ if (CollectionUtils.isEmpty(set1) && CollectionUtils.isEmpty(set2)) return Collections.EMPTY_SET; if (CollectionUtils.isEmpty(set1) || CollectionUtils.isEmpty(set2)) return Collections.EMPTY_SET; Sets.SetView<T> intersection = Sets.intersection(set1, set2); Set<T> result = intersection.stream().collect(Collectors.toSet()); return result; } /** * 差集 1中有而2中没有的 * * @param set1 set1 * @param set2 set2 * @return {@link Set}<{@link T}> */ public static <T> Set<T> difference(Set<T> set1, Set<T> set2){ if (CollectionUtils.isEmpty(set1) && CollectionUtils.isEmpty(set2)) return Collections.EMPTY_SET; if (CollectionUtils.isEmpty(set1)) return Collections.EMPTY_SET; Sets.SetView<T> difference = Sets.difference(set1, set2); Set<T> result = difference.stream().collect(Collectors.toSet()); return result; } /** * 相对差集 1中有2中没有 2中有1中没有的 取出来做结果 * * @param set1 set1 * @param set2 set2 * @return {@link Set}<{@link T}> */ public static <T> Set<T> symmetricDifference(Set<T> set1, Set<T> set2){ if (CollectionUtils.isEmpty(set1) && CollectionUtils.isEmpty(set2)) return Collections.EMPTY_SET; Sets.SetView<T> symmetricDifference = Sets.symmetricDifference(set1, set2); Set<T> result = symmetricDifference.stream().collect(Collectors.toSet()); return result; } }
pom.xml引用
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>