5
5
LinkedHashSet是一个能记录插入顺序的hashset,继承自HashSet,主要调用HashSet下面这个构造方法。
6
6
7
7
``` java
8
- HashSet(int initialCapacity, float loadFactor, boolean dummy) {
9
- map = new LinkedHashMap<> (initialCapacity, loadFactor);
10
- }
8
+ HashSet(int initialCapacity, float loadFactor, boolean dummy) {
9
+ map = new LinkedHashMap<> (initialCapacity, loadFactor);
10
+ }
11
11
```
12
12
13
13
LinkedHashSet使用LinkedHashMap实现的,LinkedHashMap又继承自HashMap,如果你看过我写的[ LinkedHashMap源码分析] ( https://wardseptember.gitee.io/mynotes/#/docs/Java%E9%9B%86%E5%90%88%E5%8C%85/LinkedHashMap%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90 ) 和[ HashSet源码分析] ( https://wardseptember.gitee.io/mynotes/#/docs/Java%E9%9B%86%E5%90%88%E5%8C%85/HashSet%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90 ) ,理解LinkedHashSet就非常容易了。
14
14
15
- <div align =" center " > <img src =" ../.. /imgs/20201206181926.png" width =" 600 " /> </div ><br >
15
+ <div align =" center " > <img src =" https://raw.githubusercontent.com/wardseptember/notes/master /imgs/20201206181926.png" width =" 600 " /> </div ><br >
16
16
17
17
LinkedHashSet是基于双向链表和HashMap实现的,HashMap的key就是LinkedHashSet就是不重复的集合,HashMap的Value指向同一个Object实例。
18
18
@@ -22,42 +22,42 @@ LinkedHashSet是基于双向链表和HashMap实现的,HashMap的key就是Linke
22
22
23
23
``` java
24
24
public class LinkedHashSet <E>
25
- extends HashSet<E >
26
- implements Set<E > , Cloneable , java.io. Serializable {
25
+ extends HashSet<E >
26
+ implements Set<E > , Cloneable , java.io. Serializable {
27
27
28
- private static final long serialVersionUID = - 2851667679971038690L ;
28
+ private static final long serialVersionUID = - 2851667679971038690L ;
29
29
30
- public LinkedHashSet (int initialCapacity , float loadFactor ) {
31
- super (initialCapacity, loadFactor, true );
32
- }
30
+ public LinkedHashSet (int initialCapacity , float loadFactor ) {
31
+ super (initialCapacity, loadFactor, true );
32
+ }
33
33
34
- public LinkedHashSet (int initialCapacity ) {
35
- super (initialCapacity, .75f , true );
36
- }
34
+ public LinkedHashSet (int initialCapacity ) {
35
+ super (initialCapacity, .75f , true );
36
+ }
37
37
38
- public LinkedHashSet () {
39
- super (16 , .75f , true );
40
- }
38
+ public LinkedHashSet () {
39
+ super (16 , .75f , true );
40
+ }
41
41
42
- public LinkedHashSet (Collection<? extends E > c ) {
43
- super (Math . max(2 * c. size(), 11 ), .75f , true );
44
- addAll(c);
45
- }
42
+ public LinkedHashSet (Collection<? extends E > c ) {
43
+ super (Math . max(2 * c. size(), 11 ), .75f , true );
44
+ addAll(c);
45
+ }
46
46
47
47
48
- @Override
49
- public Spliterator<E > spliterator () {
50
- return Spliterators . spliterator(this , Spliterator . DISTINCT | Spliterator . ORDERED );
51
- }
48
+ @Override
49
+ public Spliterator<E > spliterator () {
50
+ return Spliterators . spliterator(this , Spliterator . DISTINCT | Spliterator . ORDERED );
51
+ }
52
52
}
53
53
```
54
54
55
55
LinkedHashSet的构造方法都调用了HashSet同一个构造方法,
56
56
57
57
``` java
58
- HashSet(int initialCapacity, float loadFactor, boolean dummy) {
59
- map = new LinkedHashMap<> (initialCapacity, loadFactor);
60
- }
58
+ HashSet(int initialCapacity, float loadFactor, boolean dummy) {
59
+ map = new LinkedHashMap<> (initialCapacity, loadFactor);
60
+ }
61
61
```
62
62
63
63
LinkedHashSet是基于LinkedHashMap实现的,就是调用HashMap,看一下他的add方法吧
@@ -67,9 +67,9 @@ LinkedHashSet是基于LinkedHashMap实现的,就是调用HashMap,看一下
67
67
他的add方法就是hashset的add方法
68
68
69
69
``` java
70
- public boolean add(E e) {
71
- return map. put(e, PRESENT )== null ;
72
- }
70
+ public boolean add(E e) {
71
+ return map. put(e, PRESENT )== null ;
72
+ }
73
73
```
74
74
75
- LinkedHashMap没有覆写put方法,所以还是调用HashMap的put方法,HashMap的put方法又调用了putVal方法,putVal新增结点调用了newNode方法,LinkedHashMap覆写了newNode方法,在newNode方法中实现了链表功能。
75
+ LinkedHashMap没有覆写put方法,所以还是调用HashMap的put方法,HashMap的put方法又调用了putVal方法,putVal新增结点调用了newNode方法,LinkedHashMap覆写了newNode方法,在newNode方法中实现了链表功能。
0 commit comments