ConcurrentSkipListSet
简介
Java注释
A scalable concurrent {@link NavigableSet} implementation based on a {@link ConcurrentSkipListMap}. The elements of the set are kept sorted according to their {@linkplain Comparable natural ordering},or by a {@link Comparator} provided at set creation time, depending on which constructor is used.
翻译
基于
ConcurrentSkipListMap
的可伸缩并发NavigableSet
实现。集合的元素根据它们的可比自然排序或或在集合创建时提供的Comparator
进行排序,具体取决于使用哪个构造函数。
ConcurrentSkipListSet
底层是通过ConcurrentNavigableMap
来实现的,它是一个有序的线程安全的集合。
类图
源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
public class ConcurrentSkipListSet<E>
extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable {
//序列化ID
private static final long serialVersionUID = -2479143111061671589L;
//使用ConcurrentNavigableMap存储
private final ConcurrentNavigableMap<E,Object> m;
//无参构造方法
public ConcurrentSkipListSet() {
m = new ConcurrentSkipListMap<E,Object>();
}
//指定比较器构造方法
public ConcurrentSkipListSet(Comparator<? super E> comparator) {
m = new ConcurrentSkipListMap<E,Object>(comparator);
}
//指定集合构造
public ConcurrentSkipListSet(Collection<? extends E> c) {
m = new ConcurrentSkipListMap<E,Object>();
addAll(c);
}
//使用SortedSet构造
public ConcurrentSkipListSet(SortedSet<E> s) {
m = new ConcurrentSkipListMap<E,Object>(s.comparator());
addAll(s);
}
//使用ConcurrentNavigableMap构造
ConcurrentSkipListSet(ConcurrentNavigableMap<E,Object> m) {
this.m = m;
}
//克隆
public ConcurrentSkipListSet<E> clone() {
try {
@SuppressWarnings("unchecked")
ConcurrentSkipListSet<E> clone =
(ConcurrentSkipListSet<E>) super.clone();
clone.setMap(new ConcurrentSkipListMap<E,Object>(m));
return clone;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
/* ---------------- Set operations -------------- */
//元素个数
public int size() {
return m.size();
}
//集合是否为空
public boolean isEmpty() {
return m.isEmpty();
}
//是否包含元素o
public boolean contains(Object o) {
return m.containsKey(o);
}
//添加元素
public boolean add(E e) {
return m.putIfAbsent(e, Boolean.TRUE) == null;
}
//移除元素
public boolean remove(Object o) {
return m.remove(o, Boolean.TRUE);
}
//清空集合
public void clear() {
m.clear();
}
//迭代器
public Iterator<E> iterator() {
return m.navigableKeySet().iterator();
}
//逆序迭代器
public Iterator<E> descendingIterator() {
return m.descendingKeySet().iterator();
}
/* ---------------- AbstractSet Overrides -------------- */
//复写equals方法
public boolean equals(Object o) {
// Override AbstractSet version to avoid calling size()
if (o == this)
return true;
if (!(o instanceof Set))
return false;
Collection<?> c = (Collection<?>) o;
try {
return containsAll(c) && c.containsAll(this);
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
}
//移除c中相等元素
public boolean removeAll(Collection<?> c) {
// Override AbstractSet version to avoid unnecessary call to size()
boolean modified = false;
for (Object e : c)
if (remove(e))
modified = true;
return modified;
}
/* ---------------- Relational operations -------------- */
//小于e的最大元素
public E lower(E e) {
return m.lowerKey(e);
}
//小于等于e的最大元素
public E floor(E e) {
return m.floorKey(e);
}
//大于等于e的最小元素
public E ceiling(E e) {
return m.ceilingKey(e);
}
//大于e的最小元素
public E higher(E e) {
return m.higherKey(e);
}
//弹出最小的元素
public E pollFirst() {
Map.Entry<E,Object> e = m.pollFirstEntry();
return (e == null) ? null : e.getKey();
}
//弹出最大元素
public E pollLast() {
Map.Entry<E,Object> e = m.pollLastEntry();
return (e == null) ? null : e.getKey();
}
/* ---------------- SortedSet operations -------------- */
//比较器
public Comparator<? super E> comparator() {
return m.comparator();
}
//返回最小元素
public E first() {
return m.firstKey();
}
//返回最大元素
public E last() {
return m.lastKey();
}
//取两个元素之间的子set
public NavigableSet<E> subSet(E fromElement,
boolean fromInclusive,
E toElement,
boolean toInclusive) {
return new ConcurrentSkipListSet<E>
(m.subMap(fromElement, fromInclusive,
toElement, toInclusive));
}
//取头子set
public NavigableSet<E> headSet(E toElement, boolean inclusive) {
return new ConcurrentSkipListSet<E>(m.headMap(toElement, inclusive));
}
//取尾子set
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return new ConcurrentSkipListSet<E>(m.tailMap(fromElement, inclusive));
}
//取子set,包含from,不包含to
public NavigableSet<E> subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
//取头子set,不包含to
public NavigableSet<E> headSet(E toElement) {
return headSet(toElement, false);
}
//取尾子set,包含from
public NavigableSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
//降序set
public NavigableSet<E> descendingSet() {
return new ConcurrentSkipListSet<E>(m.descendingMap());
}
//分割的迭代器
@SuppressWarnings("unchecked")
public Spliterator<E> spliterator() {
if (m instanceof ConcurrentSkipListMap)
return ((ConcurrentSkipListMap<E,?>)m).keySpliterator();
else
return (Spliterator<E>)((ConcurrentSkipListMap.SubMap<E,?>)m).keyIterator();
}
//CAS更新map,给clone方法使用
private void setMap(ConcurrentNavigableMap<E,Object> map) {
UNSAFE.putObjectVolatile(this, mapOffset, map);
}
//CAS操作相关内容
private static final sun.misc.Unsafe UNSAFE;
private static final long mapOffset;
static {
try {
UNSAFE = sun.misc.Unsafe.getUnsafe();
Class<?> k = ConcurrentSkipListSet.class;
mapOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("m"));
} catch (Exception e) {
throw new Error(e);
}
}
}
总结
ConcurrentSkipListSet
底层是使用ConcurrentNavigableMap
实现的;ConcurrentSkipListSet
有序的,基于元素的自然排序或者通过比较器确定的顺序;ConcurrentSkipListSet
是线程安全的;
Set | 有序性 | 线程安全 | 底层实现 | 关键接口 | 特点 |
---|---|---|---|---|---|
HashSet |
无 | 否 | HashMap |
无 | 简单 |
LinkedHashSet |
有 | 否 | LinkedHashMap |
无 | 插入顺序 |
TreeSet |
有 | 否 | NavigableMap |
NavigableSet |
自然顺序 |
CopyOnWriteArraySet |
有 | 是 | CopyOnWriteArrayList |
无 | 插入顺序,读写分离 |
ConcurrentSkipListSet |
有 | 是 | ConcurrentNavigableMap |
NavigableSet |
自然顺序 |