數據結構-Set
一、Set:特點不包含重復元素
常用功能:
HashSet result = new HashSet(); HashSet set1 = new HashSet(); HashSet set2 = new HashSet(); result.addAll(set1); result.retainAll(set2);
HashSet result = new HashSet(); HashSet set1 = new HashSet(); HashSet set2 = new HashSet(); result.addAll(set1); result.removeAll(set2);
HashSet result = new HashSet(); HashSet set1 = new HashSet(); HashSet set2 = new HashSet(); result.addAll(set1); result.addAll(set2);
1、HashSet
HashSet基于散列表實現,散列表使用鏈表數組實現,每一個列表稱為桶;
散列表中可以設置初始的桶數,桶數為收集散列值(HashCode值)的桶的數量;
填裝因子默認0.75,當(表中填入的記錄數/Hash表的長度)>填裝因子時,會新建一個雙倍桶數的散列來存儲原散列的數據;
每一個對象都會根據HashCode方法生成一個HashCode值,每一個對象都會根據(HashCode值%桶)的值確定所要存入的桶中;
在使用contains方法求是否有該元素時,會依次訪問每一個桶,根據HashCode值確定元素位于哪個桶中,再查詢這個桶中的元素;

浙公網安備 33010602011771號