JAVA求字符串数组交集、并集和差集
1 package string; 2 3 import java.util.HashMap; 4 import java.util.HashSet; 5 import java.util.LinkedList; 6 import java.util.Map; 7 import java.util.Map.Entry; 8 import java.util.Set; 9 10 public class StringArray { 11 public static void main(String[] args) { 12 //测试union 13 String[] arr1 = {"abc", "df", "abc"}; 14 String[] arr2 = {"abc", "cc", "df", "d", "abc"}; 15 String[] result_union = union(arr1, arr2); 16 System.out.println("求并集的结果如下:"); 17 for (String str : result_union) { 18 System.out.println(str); 19 } 20 System.out.println("---------------------可爱的分割线------------------------"); 21 22 //测试insect 23 String[] result_insect = intersect(arr1, arr2); 24 System.out.println("求交集的结果如下:"); 25 for (String str : result_insect) { 26 System.out.println(str); 27 } 28 29 System.out.println("---------------------疯狂的分割线------------------------"); 30 //测试minus 31 String[] result_minus = minus(arr1, arr2); 32 System.out.println("求差集的结果如下:"); 33 for (String str : result_minus) { 34 System.out.println(str); 35 } 36 } 37 38 //求两个字符串数组的并集,利用set的元素唯一性 39 public static String[] union(String[] arr1, String[] arr2) { 40 Set<String> set = new HashSet<String>(); 41 for (String str : arr1) { 42 set.add(str); 43 } 44 for (String str : arr2) { 45 set.add(str); 46 } 47 String[] result = {}; 48 return set.toArray(result); 49 } 50 51 //求两个数组的交集 52 public static String[] intersect(String[] arr1, String[] arr2) { 53 Map<String, Boolean> map = new HashMap<String, Boolean>(); 54 LinkedList<String> list = new LinkedList<String>(); 55 for (String str : arr1) { 56 if (!map.containsKey(str)) { 57 map.put(str, Boolean.FALSE); 58 } 59 } 60 for (String str : arr2) { 61 if (map.containsKey(str)) { 62 map.put(str, Boolean.TRUE); 63 } 64 } 65 66 for (Entry<String, Boolean> e : map.entrySet()) { 67 if (e.getValue().equals(Boolean.TRUE)) { 68 list.add(e.getKey()); 69 } 70 } 71 72 String[] result = {}; 73 return list.toArray(result); 74 } 75 76 //求两个数组的差集 77 public static String[] minus(String[] arr1, String[] arr2) { 78 LinkedList<String> list = new LinkedList<String>(); 79 LinkedList<String> history = new LinkedList<String>(); 80 String[] longerArr = arr1; 81 String[] shorterArr = arr2; 82 //找出较长的数组来减较短的数组 83 if (arr1.length > arr2.length) { 84 longerArr = arr2; 85 shorterArr = arr1; 86 } 87 for (String str : longerArr) { 88 if (!list.contains(str)) { 89 list.add(str); 90 } 91 } 92 for (String str : shorterArr) { 93 if (list.contains(str)) { 94 history.add(str); 95 list.remove(str); 96 } else { 97 if (!history.contains(str)) { 98 list.add(str); 99 } 100 } 101 } 102 103 String[] result = {}; 104 return list.toArray(result); 105 } 106 }
程序运行,打印输出:
求并集的结果如下:
d
abc
df
cc
---------------------可爱的分割线------------------------
求交集的结果如下:
abc
df
---------------------疯狂的分割线------------------------
求差集的结果如下:
cc
d
==========================================路漫漫其修远兮========================================== ====================== 知识改变生活 ====================== ====================== 技术改变命运 ====================== ====================== 我是一名普通的程序员 ====================== ====================== 希望通过自己的努力来使自己人生更加绚烂多彩 ====================== ==========================================吾将上下而求索==========================================