/**
* Created by yuhui on 2017/7/11 0011.
*/
import java.util.HashSet;
import java.util.Set;
public class TestSet {
public static void main(String[] args) {
Set<String> result = new HashSet<String>();
Set<String> set1 = new HashSet<String>() {
{
add("王者荣耀");
add("英雄联盟");
add("穿越火线");
add("地下城与勇士");
}
};
Set<String> set2 = new HashSet<String>() {
{
add("王者荣耀");
add("地下城与勇士");
add("魔兽世界");
}
};
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);
}
}
标签:Set,JAVA,并集,HashSet,add,result,set2,set1
From: https://blog.51cto.com/u_16110904/6458130