- //记得引入包
- import java.util.*;
- public class TestSet{
- public static void main(String[] args){
- Set s1 = new HashSet();
- Set s2 = new HashSet();
- s1.add("a");s1.add("b");s1.add("c");
- s2.add("b");s2.add("c");s2.add("d");
- //把s1复制到sn中
- Set sn = new HashSet(s1);
- //求sn和s2的交集
- sn.retainAll(s2);
- Set su = new HashSet(s1);
- //吧s2中的所有元素复制到su中,但HashSet元素不能重复
- su.addAll(s2);
- System.out.println(sn);
- System.out.println(su);
- System.out.println();
- List l = new ArrayList();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/300720134883.html
来源: http://www.codesnippet.cn/detail/300720134883.html