Java Set集合的操作

xiaodai 贡献于2012-01-01

作者 qq  创建于2011-08-08 04:52:00   修改者qq  修改于2011-08-08 04:56:00字数2359

文档摘要:Set operations: union(并集), intersection(交集),asymetric difference(非对称差集), symmetric difference(对称差集), is subset(子集), is superset(超集)
关键词:

首先看一个例子: Set operations: union(并集), intersection(交集),asymetric difference(非对称差集), symmetric difference(对称差集), is subset(子集), is superset(超集) import java.util.Set; import java.util.TreeSet; public class SetTest { public static < T> Set< T> union(Set< T> setA, Set< T> setB) { Set< T> tmp = new TreeSet< T>(setA); tmp.addAll(setB); return tmp; } public static < T> Set< T> intersection(Set< T> setA, Set< T> setB) { Set< T> tmp = new TreeSet< T>(); for (T x : setA) if (setB.contains(x)) tmp.add(x); return tmp; } public static < T> Set< T> intersection2(Set< T> setA, Set< T> setB) { Set< T> tmp = new TreeSet< T>(setA); tmp.retainAll(setB); return tmp; } public static < T> Set< T> difference(Set< T> setA, Set< T> setB) { Set< T> tmp = new TreeSet< T>(setA); tmp.removeAll(setB); return tmp; } public static < T> Set< T> symDifference(Set< T> setA, Set< T> setB) { Set< T> tmpA; Set< T> tmpB; tmpA = union(setA, setB); tmpB = intersection(setA, setB); return difference(tmpA, tmpB); } public static < T> boolean isSubset(Set< T> setA, Set< T> setB) { return setB.containsAll(setA); } public static < T> boolean isSuperset(Set< T> setA, Set< T> setB) { return setA.containsAll(setB); } public static void main(String args[]) { TreeSet< Character> set1 = new TreeSet< Character>(); TreeSet< Character> set2 = new TreeSet< Character>(); set1.add('A'); set1.add('B'); set1.add('C'); set1.add('D'); set2.add('C'); set2.add('D'); set2.add('E'); set2.add('F'); System.out.println("set1: " + set1); System.out.println("set2: " + set2); System.out.println("Union: " + union(set1, set2)); System.out.println("Intersection: " + intersection(set1, set2)); System.out.println("Intersection2: " + intersection2(set1, set2)); System.out.println("Difference (set1 - set2): " + difference(set1, set2)); System.out.println("Symmetric Difference: " + symDifference(set1, set2)); TreeSet< Character> set3 = new TreeSet< Character>(set1); set3.remove('D'); System.out.println("set3: " + set3); System.out.println("Is set1 a subset of set2? " + isSubset(set1, set3)); System.out.println("Is set1 a superset of set2? " + isSuperset(set1, set3)); System.out.println("Is set3 a subset of set1? " + isSubset(set3, set1)); System.out.println("Is set3 a superset of set1? " + isSuperset(set3, set1)); } } 运行结果: C:\work>java SetTest set1: [A, B, C, D] set2: [C, D, E, F] Union: [A, B, C, D, E, F] Intersection: [C, D] Intersection2: [C, D] Difference (set1 - set2): [A, B] Symmetric Difference: [A, B, E, F] set3: [A, B, C] Is set1 a subset of set2? false Is set1 a superset of set2? true Is set3 a subset of set1? true Is set3 a superset of set1? false 这里很重要的是,一些集合操作会修改原始集合,比如addAll,retainAll,removeAll。尤其是你要重复操作原始集合的时候,一定要新建一个集合,new??TreeSet< T>(orginalSet)。

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档