java集合运算:求交集,并集,集合差,java交集,java集合运算:求交集
java集合运算:求交集,并集,集合差
[Java]代码package test;import java.util.HashSet;import java.util.Set;public class Test { public static void main(String[] args) { Set result = new HashSet(); Set set1 = new HashSet(){{ add(1); add(3); add(5); }}; Set set2 = new HashSet(){{ add(1); add(2); add(3); }}; result.clear(); result.addAll(set1); result.retainAll(set2); System.out.println("交集:"+result); result.clear(); result.addAll(set1); result.removeAll(set2); System.out.println("差集:"+result); result.clear(); result.addAll(set1); result.addAll(set2); System.out.println("并集:"+result); }}