1. HashSet,TreeSet是如何使用hashCode()和equal()方法的
2. TreeMap,TreeSet中的對象何時以及爲何要實現Comparable接口?
二. 回答:
1. HashSet是通過HashMap實現的,TreeSet是通過TreeMap實現的,只不過Set用的只是Map的key。(注意理解一下這句話,可以參考HashSet與HashMap的區別)
2. Map的key和Set都有一個共同的特性就是集合的唯一性.TreeMap更是多了一個有序性.
3. hashCode和equal()是HashMap用的, 因爲無需排序所以只需要關注定位和唯一性即可.
a. hashCode是用來計算hash值的,hash值是用來確定hash表索引的.
b. hash表中的一個索引處存放的是一張鏈表, 所以還要通過equal方法循環比較鏈上的每一個對象 纔可以真正定位到鍵值對應的Entry.
c. put時,如果hash表中沒定位到,就在鏈表前加一個Entry,如果定位到了,則更換Entry中的value,並返回舊value
d. 覆寫key的hashCode()和equal()時一定要注意,不要把它們和可變屬性關聯上,否則屬性變了之後hashCode會變,equal也會爲false, 這樣在Map中就找不不到它了,而且這樣的對象因爲找不到它所以得不到釋放,這樣就變成了一個無效引用了(相當於內存泄漏).
4. 由於TreeMap需要排序,所以需要一個Comparator爲鍵值進行大小比較.當然也是用Comparator定位的.
a. Comparator可以在創建TreeMap時指定,這時排序時使用Comparator.compare
b. 如果創建時沒有指定Comparator,那麼就會使用key.compareTo()方法,這就要求key必須實現Comparable接口.
c. TreeMap是使用Tree數據結構實現的,所以使用compare接口就可以完成定位了.
<--------------------------非常不給力的分割線--------------------------->
- import java.util.HashSet;
- import java.util.Iterator;
- public class WpsklHashSet
- {
- //java 中Set的使用(不允許有重複的對象):
- public static void main(String[] args)
- {
- HashSet hashSet=new HashSet();
- String a=new String("A");
- String b=new String("B");
- String c=new String("B");
- hashSet.add(a);
- hashSet.add(b);
- System.out.println(hashSet.size());
- String cz=hashSet.add(c)?"此對象不存在":"已經存在";
- System.out.println("測試是否可以添加對象 "+cz);
- System.out.println(hashSet.isEmpty());
- //測試其中是否已經包含某個對象
- System.out.println(hashSet.contains("A"));
- Iterator ir=hashSet.iterator();
- while(ir.hasNext())
- {
- System.out.println(ir.next());
- }
- //測試某個對象是否可以刪除
- System.out.println(hashSet.remove("a"));
- System.out.println(hashSet.remove("A"));
- //經過測試,如果你想再次使用ir變量,必須重新更新以下
- ir=hashSet.iterator();
- while(ir.hasNext())
- {
- System.out.println(ir.next());
- }
- }
- }
- /**
- * 通過這個程序,還可以測試樹集的添加元素的無序性與輸出的有序性
- */
- import java.util.TreeSet;
- import java.util.Iterator;
- public class TreeSetTest
- {
- public static void main(String[] args)
- {
- TreeSet tree = new TreeSet();
- tree.add("China");
- tree.add("America");
- tree.add("Japan");
- tree.add("Chinese");
- Iterator iter = tree.iterator();
- while(iter.hasNext())
- {
- System.out.println(iter.next());
- }
- }
- }
import java.util.HashSet;
import java.util.Iterator;
public class WpsklHashSet
{
//java 中Set的使用(不允許有重複的對象):
public static void main(String[] args)
{
HashSet hashSet=new HashSet();
String a=new String("A");
String b=new String("B");
String c=new String("B");
hashSet.add(a);
hashSet.add(b);
System.out.println(hashSet.size());
String cz=hashSet.add(c)?"此對象不存在":"已經存在";
System.out.println("測試是否可以添加對象 "+cz);
System.out.println(hashSet.isEmpty());
//測試其中是否已經包含某個對象
System.out.println(hashSet.contains("A"));
Iterator ir=hashSet.iterator();
while(ir.hasNext())
{
System.out.println(ir.next());
}
//測試某個對象是否可以刪除
System.out.println(hashSet.remove("a"));
System.out.println(hashSet.remove("A"));
//經過測試,如果你想再次使用ir變量,必須重新更新以下
ir=hashSet.iterator();
while(ir.hasNext())
{
System.out.println(ir.next());
}
}
}
/**
* 通過這個程序,還可以測試樹集的添加元素的無序性與輸出的有序性
*/
import java.util.TreeSet;
import java.util.Iterator;
public class TreeSetTest
{
public static void main(String[] args)
{
TreeSet tree = new TreeSet();
tree.add("China");
tree.add("America");
tree.add("Japan");
tree.add("Chinese");
Iterator iter = tree.iterator();
while(iter.hasNext())
{
System.out.println(iter.next());
}
}
}
另在轉一些其他的區別(感謝“baidu知道”的andygulin朋友):
1、TreeSet 是二差樹實現的,Treeset中的數據是自動排好序的,不允許放入null值。
2、HashSet 是哈希表實現的,HashSet中的數據是無序的,可以放入null,但只能放入一個null,兩者中的值都不能重複,就如數據庫中唯一約束。
3、HashSet要求放入的對象必須實現HashCode()方法,放入的對象,是以hashcode碼作爲標識的,而具有相同內容的 String對象,hashcode是一樣,所以放入的內容不能重複。但是同一個類的對象可以放入不同的實例 。