1:Set接口是Collection的子接口,Set接口没有提供额外的方法,但实现Set接口的容器类中的元素是没有顺序的,而且不可以重复。
2:Set容器可以与数学中“集合”的概念相对应。
3:J2SDK API 中所提供的 Set 容器类有 HashSet, TreeSet等。
public static void mian (String [] args) {
Set s = new HashSet();
s.add("hello");
s.add ("word");
s.add(new Name("f1","f2"));
s.add(new Integer(100));
s.add(new Name("f1","f2"));
s.add("hello");
System.out,println(s);
}
输出结果: [100, hello,word,f1 f2]
public static void main (String[] args) {
Set s1 = new HashSet();
Set s2 = new HashSet();
s1.add("a"); s1.add("b"); s1.add("c"); //s1中添加 a b c
s2.add("d"); s2.add("a"); s2.add("b"); //s2中添加 d a b
Set sn = new HashSet(s1); //s1中的元素拷贝到sn
sn.retainAll(s2); //求sn和s2的交集
Set su = new HashSet(s1);
su.addAll(s2); //求并集
System.out.println(sn);
System.out.println(su);
}