Set接口介绍
文章目錄
- 前言
- 一、Set接口:
- 1.Set接口特點(diǎn):
- 2.Set的實(shí)現(xiàn)類:
- Set接口提供的方法API:
- 二、HashSet
- 1.HashSet的特點(diǎn):
- 2.HashSet的屬性:
- 3.HashSet的構(gòu)造函數(shù):
- 4.HashSet方法:
- 三、LinkedHashSet:
- 1.LinkedHashSet的特點(diǎn):
- 2.LinkedHashSet的繼承關(guān)系和構(gòu)造器:
- 3、LinkedHastSet的應(yīng)用場(chǎng)景:
- 四、TreeSet
- 1.TreeSet的特點(diǎn):
- 2.屬性:
- 3.構(gòu)造器方法
- 總結(jié)
前言
Set接口是Collection接口的子類,其繼承了所有方法。那么他有什么獨(dú)特的特點(diǎn)呢,下來我們來研究一下Set接口以及她的實(shí)現(xiàn)類的特點(diǎn)
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、Set接口:
1.Set接口特點(diǎn):
數(shù)據(jù)是不能重復(fù)的、最多可以存儲(chǔ)一個(gè)null值
2.Set的實(shí)現(xiàn)類:
HashSet、LinkedHashSet和TreeSet
Set接口提供的方法API:
二、HashSet
1.HashSet的特點(diǎn):
1、HashSet不能保證數(shù)據(jù)有序 2、HashSet數(shù)據(jù)是不能重復(fù)的 3、HashSet是可以存儲(chǔ)null值2.HashSet的屬性:
//map屬性是存儲(chǔ)數(shù)據(jù)的,是HashMap類型的數(shù)據(jù) private transient HashMap<E,Object> map; //PRESENT屬性,HashMap中填充的value值 private static final Object PRESENT = new Object();3.HashSet的構(gòu)造函數(shù):
調(diào)用HashSet的構(gòu)造函數(shù)發(fā)現(xiàn)其底層是實(shí)現(xiàn)了一個(gè)HashMap的
public HashSet() {map = new HashMap<>();}public HashSet(Collection<? extends E> c) {map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));addAll(c);}public HashSet(int initialCapacity, float loadFactor) {map = new HashMap<>(initialCapacity, loadFactor);}public HashSet(int initialCapacity) {map = new HashMap<>(initialCapacity);}4.HashSet方法:
public boolean add(E e) {return map.put(e, PRESENT)==null;}public boolean remove(Object o) {return map.remove(o)==PRESENT;}public void clear() {map.clear();}由上面的源碼我們可以發(fā)現(xiàn)HashSet底層實(shí)現(xiàn)是基于HashMap來實(shí)現(xiàn)的,將set中存儲(chǔ)的值作為HashMap的key來處理,PRESENT是一個(gè)填充的value值
三、LinkedHashSet:
1.LinkedHashSet的特點(diǎn):
1、數(shù)據(jù)有序 2、數(shù)據(jù)不能重復(fù) 3、數(shù)據(jù)是可以存儲(chǔ)null的2.LinkedHashSet的繼承關(guān)系和構(gòu)造器:
通過聲明形式可知:其繼承自HashSet,其繼承了HashSet中所有的屬性和方法
public class LinkedHashSet<E> extends HashSet<E>implements Set<E>, Cloneable, java.io.Serializable {public LinkedHashSet(int initialCapacity, float loadFactor) {super(initialCapacity, loadFactor, true);}public LinkedHashSet(int initialCapacity) {super(initialCapacity, .75f, true);}public LinkedHashSet() {super(16, .75f, true);}public LinkedHashSet(Collection<? extends E> c) {super(Math.max(2*c.size(), 11), .75f, true);addAll(c);} }通過聲明形式可知:其繼承自HashSet,其繼承了HashSet中所有的屬性和方法
構(gòu)造函數(shù)調(diào)用父類HashSet中方法如下:
LinkedHashSet的實(shí)現(xiàn)是基于LinkedHashMap來實(shí)現(xiàn)的
LinkedHashSet數(shù)據(jù)有序的特征是基于LinkedHashMap來保證的,其底層利用雙向鏈表來實(shí)現(xiàn)的數(shù)據(jù)有序
3、LinkedHastSet的應(yīng)用場(chǎng)景:
在去重的基礎(chǔ)上數(shù)據(jù)有序
四、TreeSet
1.TreeSet的特點(diǎn):
1、數(shù)據(jù)自然有序(自定義排序,實(shí)現(xiàn)Comparator接口)
2、數(shù)據(jù)不能重復(fù)
3、數(shù)據(jù)不能為null
2.屬性:
//TreeSet實(shí)現(xiàn)類的一共父接口實(shí)例用來存儲(chǔ)數(shù)據(jù)private transient NavigableMap<E,Object> m;//PRESENT屬性,HashMap中填充的value值 private static final Object PRESENT = new Object();3.構(gòu)造器方法
通過源碼可知:TreeSet底層是基于treeMap來實(shí)現(xiàn)的
TreeSet(NavigableMap<E,Object> m) {this.m = m;}public TreeSet() {this(new TreeMap<>());}public TreeSet(Comparator<? super E> comparator) {this(new TreeMap<>(comparator));}public TreeSet(Collection<? extends E> c) {this();addAll(c);}public TreeSet(SortedSet<E> s) {this(s.comparator());addAll(s);}總結(jié)
當(dāng)我們學(xué)習(xí)完Set的接口和他的實(shí)現(xiàn)類以后發(fā)現(xiàn)他們的實(shí)現(xiàn)都是基于對(duì)應(yīng)Map的來實(shí)現(xiàn),他的存在是為了我們只需要進(jìn)行對(duì)單一數(shù)據(jù)操作來保證數(shù)據(jù)不重復(fù)等特點(diǎn)的使用的。
總結(jié)
- 上一篇: WeakHashMap和四种引用总结:
- 下一篇: Queue接口及是实现类Priority