set Posted on 2018-03-14 123456789101112131415161718192021222324252627282930import java.util.*; public static void main(String[] args) { Set<Integer> result = new HashSet<Integer>(); Set<Integer> set1 = new HashSet<Integer>(){{ add(1); add(3); add(5); }}; Set<Integer> set2 = new HashSet<Integer>(){{ 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);