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

 

原文引用:http://blog.sina.com.cn/s/blog_4934a04a0100fqmf.html

posted @ 2016-02-03 10:41  爱茹一婉年  阅读(20193)  评论(0编辑  收藏  举报