java集合
目录
1、List
1.1、ArrayList
1.2、LinkedList
1.3、ArrayList和LinkedList的效率?
2、Map
2.1、HashMap
2.2、LinkedHashMap
2.3、TreeMap
2.4、ConcurrentHashMap
2.5、总结
3、Set
3.1、HashSet
3.2、TreeSet
3.3、LinkedHashSet
1、List
1.1、ArrayList
底层数据结构是动态数组,ArrayList是一个泛型类,可以存放任意类型的对象。
ArrayList的所有方法都是默认在单一线程下进行的,因此ArrayList不具有线程安全性。若想在多线程下使用,应该使用Colletions类中的静态方法synchronizedList()对ArrayList进行调用即可。
1.2、LinkedList
底层数据结构是双向链表,所有的操作都可以认为是一个双向链表的操作,因为它实现了Deque接口和List接口。同样,LinkedList也是线程不安全的,如果在并发环境下使用它,同样用Colletions类中的静态方法synchronizedList()对LinkedList进行调用即可。
在LinkedList的内部实现中,是使用结点 来存放数据的,有一个指向链表头的结点first和一个指向链表尾的结点last。不同于ArrayList只能在数组末尾添加数据,LinkList可以很方便在链表头或者链表尾插入数据,或者在指定结点前后插入数据,还提供了取走链表头或链表尾的结点,或取走中间某个结点,还可以查询某个结点是否存在。add()方法默认在链表尾部插入数据。总之,LinkedList提供了大量方便的操作方法,并且它的插入或增加等方法的效率明显高于ArrayList类型,但是查询的效率要低一点,因为它是一个双向链表。
1.3、ArrayList和LinkedList的效率?
-
1、 当对其访问(get/set操作时) 的时候,ArrayList效率比较高,因为LinkedList是线性的数据存储方式,所以需要移动指针从前往后依次查找。
-
2、当对数据进行增加和删除的操作(add和remove操作)时,LinkedList比ArrayList的效率更高,因为ArrayList是数组,所以在其中进行增删操作时,会对操作点之后所有数据的下标索引造成影响,需要进行数据的移动。
-
3、ArrayList自由性较低,因为它需要手动的设置固定大小的容量,但是它的使用比较方便,只需要创建,然后添加数据,通过调用下标进行使用;而LinkedList自由性较高,能够动态的随数据量的变化而变化,但是它不便于使用。
-
4、ArrayList主要控件开销在于需要在lList列表预留一定空间;而LinkList主要控件开销在于需要存储结点信息以及结点指针信息。
2、Map
Map接口有四个比较重要的实现类,分别是HashMap、LinkedHashMap、TreeMap和HashTable。
2.1、HashMap
- 1、HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
- 2、HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度。
- 3、最多允许一条记录的键为 null,多个键值为null,不支持线程同步。
- 4、HashMap 是无序的,即不会记录插入的顺序。
- 5、通过EntrySet 对HashMap进行遍历。
底层原理
JDK1.7底层 数组 + 链表
结构图:
HashMap 中核心的成员变量:
-
1、初始化桶大小,底层是数组,数组默认的大小为16。
-
2、桶可以达到的最大值。
-
3、默认的负载因子(0.75)
-
4、table 真正存放数据的数组。
-
5、Map 已经存放数量的大小。
-
6、阈值,当Map中存放的数量大于threshold,需要resize。threshold=capacity*loadFactor。
-
7、负载因子,可在初始化时显式指定。
给定的默认容量为 16,负载因子为 0.75。Map 在使用过程中不断的往里面存放数据,当数量达到了 16 * 0.75 = 12 就需要将当前 16 的容量进行扩容,而扩容这个过程涉及到 rehash、复制数据等操作,所以非常消耗性能。
put()方法
1 public V put(K key, V value) {
2 if (table == EMPTY_TABLE) {
3 inflateTable(threshold);
4 }
5 if (key == null)
6 return putForNullKey(value);
7 int hash = hash(key);
8 int i = indexFor(hash, table.length);
9 for (Entry<K,V> e = table[i]; e != null; e = e.next) {
10 Object k;
11 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
12 V oldValue = e.value;
13 e.value = value;
14 e.recordAccess(this);
15 return oldValue;
16 }
17 }
18
19 modCount++;
20 addEntry(hash, key, value, i);
21 return null;
22 }
-
1、判断当前数组是否需要初始化。
-
2、如果 key 为空,则 put 一个空值进去。
-
3、根据 key 计算出 hashcode。
-
4、根据计算出的 hashcode 定位出所在桶。
-
5、如果桶是一个链表则需要遍历判断里面的 hashcode、key 是否和传入 key 相等,如果相等则进行覆盖,并返回原来的值。
-
6、如果桶是空的,说明当前位置没有数据存入;新增一个 Entry 对象写入当前位置。
当调用 addEntry 写入 Entry 时需要判断是否需要扩容。
如果需要就进行两倍扩充,并将当前的 key 重新 hash 并定位。
而在 createEntry 中会将当前位置的桶传入到新建的桶中,如果当前桶有值就会在位置形成链表。
1 void addEntry(int hash, K key, V value, int bucketIndex) {
2 if ((size >= threshold) && (null != table[bucketIndex])) {
3 resize(2 * table.length);
4 hash = (null != key) ? hash(key) : 0;
5 bucketIndex = indexFor(hash, table.length);
6 }
7
8 createEntry(hash, key, value, bucketIndex);
9 }
10
11 void createEntry(int hash, K key, V value, int bucketIndex) {
12 Entry<K,V> e = table[bucketIndex];
13 table[bucketIndex] = new Entry<>(hash, key, value, e);
14 size++;
15 }
get()方法
1 public V get(Object key) {
2 if (key == null)
3 return getForNullKey();
4 Entry<K,V> entry = getEntry(key);
5
6 return null == entry ? null : entry.getValue();
7 }
8
9 final Entry<K,V> getEntry(Object key) {
10 if (size == 0) {
11 return null;
12 }
13
14 int hash = (key == null) ? 0 : hash(key);
15 for (Entry<K,V> e = table[indexFor(hash, table.length)];
16 e != null;
17 e = e.next) {
18 Object k;
19 if (e.hash == hash &&
20 ((k = e.key) == key || (key != null && key.equals(k))))
21 return e;
22 }
23 return null;
24 }
-
1、首先也是根据 key 计算出 hashcode,然后定位到具体的桶中。
-
2、判断该位置是否为链表。
-
3、不是链表就根据 key、key 的 hashcode 是否相等来返回值。
-
4、为链表则需要遍历直到 key 及 hashcode 相等时候就返回值。
-
5、啥都没取到就直接返回 null 。
在JDK1.7的时候,存在问题是当 Hash 冲突严重时,在桶上形成的链表会变的越来越长,这样在查询时的效率就会越来越低;时间复杂度达到 O(N)。
JDK1.8 数组 + 链表 + 红黑树
JDK1.8 重点优化了这个查询效率。
结构图:
几个核心的成员变量:
1 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
2
3 /**
4 * The maximum capacity, used if a higher value is implicitly specified
5 * by either of the constructors with arguments.
6 * MUST be a power of two <= 1<<30.
7 */
8 static final int MAXIMUM_CAPACITY = 1 << 30;
9
10 /**
11 * The load factor used when none specified in constructor.
12 */
13 static final float DEFAULT_LOAD_FACTOR = 0.75f;
14
15 static final int TREEIFY_THRESHOLD = 8;
16
17 transient Node<K,V>[] table;
18
19 /**
20 * Holds cached entrySet(). Note that AbstractMap fields are used
21 * for keySet() and values().
22 */
23 transient Set<Map.Entry<K,V>> entrySet;
24
25 /**
26 * The number of key-value mappings contained in this map.
27 */
28 transient int size;
和 1.7 大体上都差不多,还是有几个重要的区别:
-
TREEIFY_THRESHOLD 用于判断是否需要将链表转换为红黑树的阈值 8 。
-
HashEntry 修改为 Node。
put()方法
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
-
1、判断当前桶是否为空,空的就需要初始化(resize 中会判断是否进行初始化)。
-
2、根据当前 key 的 hashcode 定位到具体的桶中并判断是否为空,为空表明没有 Hash 冲突就直接在当前位置创建一个新桶即可。
-
3、如果当前桶有值( Hash 冲突),那么就要比较当前桶中的 key、key 的 hashcode 与写入的 key 是否相等,相等就赋值给 e,在第 8 步的时候会统一进行赋值及返回。
-
4、如果当前桶为红黑树,那就要按照红黑树的方式写入数据。
-
5、如果是个链表,就需要将当前的 key、value 封装成一个新节点写入到当前桶的后面(形成链表)。
-
6、接着判断当前链表的大小是否大于预设的阈值,大于时就要转换为红黑树。
-
7、如果在遍历过程中找到 key 相同时直接退出遍历。
-
8、如果 e != null 就相当于存在相同的 key,那就需要将值覆盖。
-
9、最后判断是否需要进行扩容。
get()方法
1 public V get(Object key) {
2 Node<K,V> e;
3 return (e = getNode(hash(key), key)) == null ? null : e.value;
4 }
5
6 final Node<K,V> getNode(int hash, Object key) {
7 Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
8 if ((tab = table) != null && (n = tab.length) > 0 &&
9 (first = tab[(n - 1) & hash]) != null) {
10 if (first.hash == hash && // always check first node
11 ((k = first.key) == key || (key != null && key.equals(k))))
12 return first;
13 if ((e = first.next) != null) {
14 if (first instanceof TreeNode)
15 return ((TreeNode<K,V>)first).getTreeNode(hash, key);
16 do {
17 if (e.hash == hash &&
18 ((k = e.key) == key || (key != null && key.equals(k))))
19 return e;
20 } while ((e = e.next) != null);
21 }
22 }
23 return null;
24 }
-
1、首先将 key hash 之后取得所定位的桶。
-
2、如果桶为空则直接返回 null 。
-
3、否则判断桶的第一个位置(有可能是链表、红黑树)的 key 是否为查询的 key,是就直接返回 value。
-
4、如果第一个不匹配,则判断它的下一个是红黑树还是链表。
-
5、红黑树就按照树的查找方式返回值。
-
6、不然就按照链表的方式遍历匹配返回值。
HashMap 原有的问题也都存在,比如在并发场景下使用时容易出现死循环。
1final HashMap<String, String> map = new HashMap<String, String>();
2for (int i = 0; i < 1000; i++) {
3 new Thread(new Runnable() {
4 @Override
5 public void run() {
6 map.put(UUID.randomUUID().toString(), "");
7 }
8 }).start();
9}
HashMap的方法
总结 HashMap:无论是 1.7 还是 1.8 其实都能看出 JDK 没有对它做任何的同步操作,所以并发会出问题,甚至出现死循环导致系统不可用。
2.2、LinkedHashMap
LinkedHashMap 继承自 HashMap,LinkedHashMap 存储结构和 HashMap 相同,依然是数组+链表+红黑树。
它内部维持双向链表 , 维护插入节点的顺序,保持Key插入的顺序。在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的;
2.3、TreeMap
TreeMap实现SortMap接口,能够把它保存的记录根据键排序,默认是按键值的升序排序,也可以指定排序的比较器。当用Iterator遍历TreeMap时,得到的记录是排过序的。键、值都不能为null值,效率比HashMap低,线程不安全。
TreeMap底层结构是桶+红黑树的实现方式.
TreeMap的底层结构就是一个数组,数组中每一个元素又是一个红黑树.当添加一个元素(key-value)的时候,根据key的hash值来确定插入到哪一个桶中(确定插入数组中的位置),当桶中有多个元素时,使用红黑树进行保存;当一个桶中存放的数据过多,那么根据key查找的效率就会降低。进行扩容。
hash数组的默认大小是11,当hash数组的容量超过初始容量0.75时,增加的方式是old*2+1。
2.4、ConcurrentHashMap
JDK1.7
-
ConcurrentHashMap 类中包含 两个静态内部类HashEntry 和 Segment
-
HashEntry 用来封装映射表的键 / 值对;Segment 用来充当锁的角色,每个 Segment 对象守护整个散列映射表的若干个桶。 每个桶是由若干个 HashEntry对象链接起来的链表。
一个 ConcurrentHashMap 实例中包含由若干个 Segment 对象组成的数组。
结构图:
如图所示,是由 Segment 数组、HashEntry 组成,是数组 + 链表。
1 /**
2 * Segment 数组,存放数据时首先需要定位到具体的 Segment 中。
3 */
4 final Segment<K,V>[] segments;
5
6 transient Set<K> keySet;
7 transient Set<Map.Entry<K,V>> entrySet;
Segment主要组成如下:
1 static final class Segment<K,V> extends ReentrantLock implements Serializable {
2
3 private static final long serialVersionUID = 2249069246763182397L;
4
5 // 和 HashMap 中的 hashEntry 作用一样,真正存放数据的桶
6 transient volatile hashEntry<K,V>[] table;
7 // count 来表示本 Segment 中包含的 HashEntry 对象的总数
8 transient int count;
9 // table被更新的次数
10 transient int modCount;
11 // 当hashEntry对象的个数超过threshold时,进行再散列
12 transient int threshold;
13 // 装载因子
14 final float loadFactor;
15
16 }
Segment 类继承于 ReentrantLock 类,从而使得 Segment 对象能充当锁的角色。每个 Segment 对象用来守护其(成员对象 table 中)包含的若干个桶。
table 是一个由 HashEntry 对象组成的数组。table 数组的每一个数组成员就是散列映射表的一个桶。
HashEntry的组成:
static final class HashEntry<K,V>
final int hash;
final K key;
volatile v value;
volatile HashEntry<k,V> next;
HashEntry(int hash,K key , v value,HashEntry<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
}
在 ConcurrentHashMap 中,在散列时如果产生 “碰撞”,将采用 分离链接法 来处理“碰撞”:把“碰撞”的 HashEntry 对象链接成一个链表。由于 HashEntry 的 next 域为 final 型,所以新节点只能在链表的表头处插入。
put()方法
1 public V put(K key, V value) {
2 Segment<K,V> s;
3 if (value == null)
4 throw new NullPointerException();
5 int hash = hash(key);
6 int j = (hash >>> segmentShift) & segmentMask;
7 if ((s = (Segment<K,V>)UNSAFE.getObject // nonvolatile; recheck
8 (segments, (j << SSHIFT) + SBASE)) == null) // in ensureSegment
9 s = ensureSegment(j);
10 return s.put(key, hash, value, false);
11 }
首先是通过 key 定位到 Segment,之后在对应的 Segment 中进行具体的 put。
1 final V put(K key, int hash, V value, boolean onlyIfAbsent) {
2 HashEntry<K,V> node = tryLock() ? null :
3 scanAndLockForPut(key, hash, value);
4 V oldValue;
5 try {
6 HashEntry<K,V>[] tab = table;
7 int index = (tab.length - 1) & hash;
8 HashEntry<K,V> first = entryAt(tab, index);
9 for (HashEntry<K,V> e = first;;) {
10 if (e != null) {
11 K k;
12 if ((k = e.key) == key ||
13 (e.hash == hash && key.equals(k))) {
14 oldValue = e.value;
15 if (!onlyIfAbsent) {
16 e.value = value;
17 ++modCount;
18 }
19 break;
20 }
21 e = e.next;
22 }
23 else {
24 if (node != null)
25 node.setNext(first);
26 else
27 node = new HashEntry<K,V>(hash, key, value, first);
28 int c = count + 1;
29 if (c > threshold && tab.length < MAXIMUM_CAPACITY)
30 rehash(node);
31 else
32 setEntryAt(tab, index, node);
33 ++modCount;
34 count = c;
35 oldValue = null;
36 break;
37 }
38 }
39 } finally {
40 unlock();
41 }
42 return oldValue;
43 }
虽然 HashEntry 中的 value 是用 volatile 关键词修饰的,但是并不能保证并发的原子性,所以 put 操作时仍然需要加锁处理。
首先第一步的时候会尝试获取锁,如果获取失败肯定就有其他线程存在竞争,则利用 scanAndLockForPut() 自旋获取锁。
put 的流程:
-
1、将当前 Segment 中的 table 通过 key 的 hashcode 定位到 HashEntry。
-
2、遍历该 HashEntry,如果不为空则判断传入的 key 和当前遍历的 key 是否相等,相等则覆盖旧的 value。
-
3、不为空则需要新建一个 HashEntry 并加入到 Segment 中,同时会先判断是否需要扩容。
-
4、最后会解除在 1 中所获取当前 Segment 的锁。
get()方法
1 public V get(Object key) {
2 Segment<K,V> s; // manually integrate access methods to reduce overhead
3 HashEntry<K,V>[] tab;
4 int h = hash(key);
5 long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
6 if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
7 (tab = s.table) != null) {
8 for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
9 (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
10 e != null; e = e.next) {
11 K k;
12 if ((k = e.key) == key || (e.hash == h && key.equals(k)))
13 return e.value;
14 }
15 }
16 return null;
17 }
get 逻辑:
-
1、只需要将 Key 通过 Hash 之后定位到具体的 Segment ,再通过一次 Hash 定位到具体的元素上。
-
2、由于 HashEntry 中的 value 属性是用 volatile 关键词修饰的,保证了内存可见性,所以每次获取时都是最新值。
-
3、ConcurrentHashMap 的 get 方法是非常高效的,因为整个过程都不需要加锁。
原理上来说:ConcurrentHashMap 采用了分段锁技术,其中 Segment 继承于 ReentrantLock。 不会像 HashTable 那样不管是 put 还是 get 操作都需要做同步处理,理论上 ConcurrentHashMap 支持 CurrencyLevel (Segment 数组数量)的线程并发。每当一个线程占用锁访问一个 Segment 时,不会影响到其他的 Segment。
JDK1.8
在JDK1.7中解决了并发的问题,但是查询效率较低。
其中抛弃了原有的 Segment 分段锁,而采用了 CAS + synchronized 来保证并发安全性。
Node的组成:
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
volatile V val;
volatile Node<K,V> next;
Node(int hash, K key, V val, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.val = val;
this.next = next;
}
}
将 1.7 中存放数据的 HashEntry 改为 Node,但作用都是相同的。
其中的 val next 都用了 volatile 修饰,保证了可见性。
ConcurrentHashMap和 HashMap 非常类似,唯一的区别就是其中的核心数据如 value ,以及链表都是 volatile 修饰的,保证了获取时的可见性。
put()方法
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
if (tab == null || (n = tab.length) == 0)
tab = initTable();
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) {
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
for (Node<K,V> e = f;; ++binCount) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {
Node<K,V> p;
binCount = 2;
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
addCount(1L, binCount);
return null;
}
-
根据 key 计算出 hashcode 。
-
判断是否需要进行初始化。
-
f 即为当前 key 定位出的 Node,如果为空表示当前位置可以写入数据,利用 CAS 尝试写入,失败则自旋保证成功。
-
如果当前位置的 hashcode == MOVED == -1,则需要进行扩容。
-
如果都不满足,则利用 synchronized 锁写入数据。
-
如果数量大于 TREEIFY_THRESHOLD 则要转换为红黑树。
get()方法
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
-
根据计算出来的 hashcode 寻址,如果就在桶上那么直接返回值。
-
如果是红黑树那就按照树的方式获取值。
-
就不满足那就按照链表的方式遍历获取值。
1.8 在 1.7 的数据结构上做了大的改动,采用红黑树之后可以保证查询效率(O(logn)),甚至取消了 ReentrantLock 改为了 synchronized.。
2.5、总结
一般情况下,用的最多的是HasMap,在Map中插入,删除和定位元素,HashMap是最好的选择。需要保证线程并发安全,使用ConcurrentHasMap。
但如果要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。
如果需要输出的顺序和输入的相同,那么用LinkedHashMap可以实现,它还可以按读取顺序来排列。
3、Set
HashSet、TreeSet和LinkedHashSet,其内部都是基于Map来实现的。三者都属于Set的范畴,都是没有重复元素的集合。基于HashMap和TreeMap,所以都是非线程安全的。
3.1、HashSet
HashSet 基于 HashMap 来实现的,只不过Set用的只是Map的key, 允许有 null 值,是无序的。
3.2、TreeSet
TreeMap是一个有序的二叉树,TreeSet同样也是一个有序的,它的作用是提供有序的Set集合。
3.3、LinkedHashSet
LinkedHashSet是HashSet的一个“扩展版本”,HashSet并不管什么顺序,不同的是LinkedHashSet会维护“插入顺序”。HashSet内部使用HashMap对象来存储它的元素,而LinkedHashSet内部使用LinkedHashMap对象来存储和处理它的元素。
TreeSet和LinkedHashSet分别使用了TreeMap和LinkedHashMap来控制访问数据的有序性。