一.介紹
Set注重獨一無二的性質(zhì),該體系集合可以知道某物是否已近存在于集合中,不會存儲重復(fù)的元素,用于存儲無序(存入和取出的順序不一定相同)元素,值不能重復(fù)。
Set集合里多個對象之間沒有明顯的順序?;九cCollection方法相同。只是行為不同(Set不允許包含重復(fù)元素)。
Set集合不允許重復(fù)元素,是因為Set判斷兩個對象相同不是使用==運算符,而是根據(jù)equals方法。即兩個對象用equals方法比較返回true,Set就不能接受兩個對象。
Set是最簡單的一種集合,它的對象不按特定方式排序,只是簡單的把對象加入集合中,就像往口袋里放東西。
對Set中成員的訪問和操作是通過集中對象的引用進行的,所以Set中不能有重復(fù)對象。
Set也有多種變體,可以實現(xiàn)排序等功能,如TreeSet,它把對象添加到TreeSet中的操作將變?yōu)榘凑漳撤N比較規(guī)則將其插入到有序的對象序列中。它實現(xiàn)的是SortedSet接口,也就是加入了對象比較的方法。通過對集中的對象迭代,我們可以得到一個升序的對象集合。

二.知識點介紹
1、HashSet
2、LinkedHashSet
3、TreeSet
三.上課對應(yīng)視頻的說明文檔
1、HashSet
雖然Set同List都實現(xiàn)了Collection接口,但是他們的實現(xiàn)方式卻大不一樣。List基本上都是以Array為基礎(chǔ)。但是Set則是在 HashMap的基礎(chǔ)上來實現(xiàn)的,這個就是Set和List的根本區(qū)別。HashSet的存儲方式是把HashMap中的Key作為Set的對應(yīng)存儲項??纯?HashSet的add(Object obj)方法的實現(xiàn)就可以一目了然了。
代碼示例:
import java.util.*;
public class HTest{
public static void main(String[] args) {?
//創(chuàng)建Set集合對象?
Set<String> set = new HashSet<String>() ;?
//添加元素(增)?
set.add("hello");?
set.add("java") ;?
set.add("java") ;?
set.add("world") ;?
set.add("world") ;?
set.add("world") ;?
System.out.println("集合中添加的元素:"+set);//打印的
是內(nèi)容(原因:傳入?yún)?shù)類型是String,底層重寫了toString()方法)?
System.out.println("集合中元素的個數(shù):"+set.size());?
//增強for遍歷?
Iterator i=set.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
//刪除元素(刪)?
System.out.println("是否成功刪除:"+set.remove
("hello"));//刪除是否成功?
System.out.println("刪除后集合中的元素:"+set);?
//查詢?
System.out.println("是否包含此元素:"+set.contains
("hello"));
}
}
class Book{?
String name;?
double price;?
public Book(String name,double price) {?
// TODO Auto-generated constructor stub?
this.name = name;?
this.price = price;?
}? ? ?
@Override?
public String toString() {?
// TODO Auto-generated method stub?
return "[書名:" + this.name + " 價格:" + this.price + "]";?
}?
@Override?
public int hashCode() {?
// TODO Auto-generated method stub?
return this.name.hashCode();?
}?
@Override?
public boolean equals(Object obj) {?
// TODO Auto-generated method stub?
Book b = (Book) obj;?
return this.name.equals(b.name);?
}?
}?
public class demo2 {?
public static void main(String[] args) {?
// TODO Auto-generated method stub?
//不允許重復(fù),增加自定義對象?
HashSet<Book> books = new HashSet<Book>();?
books.add(new Book("深入Javaweb",34));?
books.add(new Book("java神書",78));?
books.add(new Book("java神書",78));?
//? ? ? books.remove(new Book("java神書",78));?
//修改書名?
Iterator<Book> it = books.iterator();?
while(it.hasNext()){?
Book b = it.next();?
if(b.name.equals("java神書")){?
b.name = "java編程思想";?
}?
}?
//為什么改了名字后不能刪除了!?
books.remove(new Book("java編程思想",78));?
System.out.println("集合的元素: "+ books);?
}?
}?
2、LinkedHashSet
LinkedHashSet是有序的而且不能重復(fù),是HashSet的一個子類,一個鏈表; 以元素插入的順序來維護集合的鏈接表,允許以插入的順序在集合中迭代;
代碼示例:
class Student{?
private int age;?
private String name;?
public Student(int age,String name)?
{?
this.age = age;?
this.name = name;?
}?
//要顯示Student類的信息,必須重寫toString 方法?
public String toString(){?
return? "age :"+age+"? name:"+name;?
}?
public int hashCode()?
{?
return age*name.hashCode();?
}?
public boolean equals(Object o){?
Student s = (Student) o;?
return age == s.age && name.equalsIgnoreCase(s.name);?
}?
}?
public class LinkedHashSetTest {?
public static void main(String[] args) {?
// TODO Auto-generated method stub?
LinkedHashSet linkHashSet =? new LinkedHashSet();?
Student linkedstu1 = new Student(18,"zxx");?
Student linkedstu2 = new Student(23,"zyj");?
Student linkedstu3 = new Student(25,"xmh");?
Student linkedstu4 = new Student(25,"zah");?
Student linkedstu5 = new Student(25,"zah");?
linkHashSet.add(linkedstu3);?
linkHashSet.add(linkedstu4);?
linkHashSet.add(linkedstu1);?
linkHashSet.add(linkedstu2);?
linkHashSet.add(linkedstu5);?
linkHashSet.add(null);?
Iterator it =? linkHashSet.iterator();?
while(it.hasNext())?
{?
System.out.println(it.next());?
}?
//經(jīng)過測試是有序的而且不能重復(fù)?
}?
}?
3、TreeSet
SortedSet的子類,它不同于HashSet的根本就是TreeSet是有序的。它是通過SortedMap來實現(xiàn)的。
TreeSet提供一個使用樹結(jié)構(gòu)存儲Set接口的實現(xiàn),對象以升序順序存儲,訪問和遍歷的時間很快。
代碼示例:
import java.util.*;
public class TreeSetDemo {
public static void main(String args[]) {
// Create a tree set
TreeSet ts = new TreeSet();
// Add elements to the tree set
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
ts.add("D");
System.out.println(ts);
}
}
綜合案例:
import java.util.HashSet;?
import java.util.LinkedHashSet;?
import java.util.TreeSet;?
public class SetDemo {?
public static void main(String[] args) {?
HashSet<String> hs = new HashSet<String>();?
//增加元素
hs.add("B");?
hs.add("A");?
hs.add("D");?
hs.add("E");?
hs.add("C");?
hs.add("F");?
System.out.println("HashSet 順序:\n"+hs);?
LinkedHashSet<String> lhs = new LinkedHashSet<String>();?
lhs.add("B");?
lhs.add("A");?
lhs.add("D");?
lhs.add("E");?
lhs.add("C");?
lhs.add("F");?
System.out.println("LinkedHashSet 順序:\n"+lhs);?
TreeSet<String> ts = new TreeSet<String>();?
ts.add("B");?
ts.add("A");?
ts.add("D");?
ts.add("E");?
ts.add("C");?
ts.add("F");?
System.out.println("TreeSet 順序:\n"+ts);?
}?
}