Skip to content

Commit b15a55c

Browse files
committed
Use ConcurrentHashMap.keySet() for more efficient implemenation of ConcurrentHashSet
1 parent 192258b commit b15a55c

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/main/java/net/greghaines/jesque/utils/ConcurrentHashSet.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.Collection;
1818
import java.util.Iterator;
1919
import java.util.concurrent.ConcurrentHashMap;
20-
import java.util.concurrent.ConcurrentMap;
2120

2221
/**
2322
* An implementation of ConcurrentSet that is backed by a ConcurrentHashMap.
@@ -33,14 +32,14 @@ private enum Nothing {
3332
NOTHING;
3433
}
3534

36-
private final ConcurrentMap<E, Nothing> delegate;
35+
private final ConcurrentHashMap.KeySetView<E, Nothing> delegate;
3736

3837
/**
3938
* Creates a new, empty set with a default initial capacity (16), load factor (0.75) and
4039
* concurrencyLevel (16).
4140
*/
4241
public ConcurrentHashSet() {
43-
this.delegate = new ConcurrentHashMap<E, Nothing>();
42+
this.delegate = new ConcurrentHashMap<E, Nothing>().keySet(Nothing.NOTHING);
4443
}
4544

4645
/**
@@ -51,7 +50,7 @@ public ConcurrentHashSet() {
5150
* accommodate this many elements.
5251
*/
5352
public ConcurrentHashSet(final int initialCapacity) {
54-
this.delegate = new ConcurrentHashMap<E, Nothing>(initialCapacity);
53+
this.delegate = new ConcurrentHashMap<E, Nothing>(initialCapacity).keySet(Nothing.NOTHING);
5554
}
5655

5756
/**
@@ -64,7 +63,8 @@ public ConcurrentHashSet(final int initialCapacity) {
6463
* performed when the average number of elements per bin exceeds this threshold.
6564
*/
6665
public ConcurrentHashSet(final int initialCapacity, final float loadFactor) {
67-
this.delegate = new ConcurrentHashMap<E, Nothing>(initialCapacity, loadFactor);
66+
this.delegate =
67+
new ConcurrentHashMap<E, Nothing>(initialCapacity, loadFactor).keySet(Nothing.NOTHING);
6868
}
6969

7070
/**
@@ -81,7 +81,8 @@ public ConcurrentHashSet(final int initialCapacity, final float loadFactor) {
8181
public ConcurrentHashSet(
8282
final int initialCapacity, final float loadFactor, final int concurrencyLevel) {
8383
this.delegate =
84-
new ConcurrentHashMap<E, Nothing>(initialCapacity, loadFactor, concurrencyLevel);
84+
new ConcurrentHashMap<E, Nothing>(initialCapacity, loadFactor, concurrencyLevel)
85+
.keySet(Nothing.NOTHING);
8586
}
8687

8788
/**
@@ -96,10 +97,9 @@ public ConcurrentHashSet(final Collection<? extends E> collection) {
9697
throw new IllegalArgumentException("collection must not be null");
9798
}
9899
this.delegate =
99-
new ConcurrentHashMap<E, Nothing>(Math.max(16, Math.round(collection.size() * 1.5f)));
100-
for (final E elem : collection) {
101-
this.delegate.put(elem, Nothing.NOTHING);
102-
}
100+
new ConcurrentHashMap<E, Nothing>(Math.max(16, Math.round(collection.size() * 1.5f)))
101+
.keySet(Nothing.NOTHING);
102+
this.delegate.addAll(collection);
103103
}
104104

105105
/** {@inheritDoc} */
@@ -117,65 +117,61 @@ public boolean isEmpty() {
117117
/** {@inheritDoc} */
118118
@Override
119119
public boolean contains(final Object obj) {
120-
return this.delegate.containsKey(obj);
120+
return this.delegate.contains(obj);
121121
}
122122

123123
/** {@inheritDoc} */
124124
@Override
125125
public Iterator<E> iterator() {
126-
return this.delegate.keySet().iterator();
126+
return this.delegate.iterator();
127127
}
128128

129129
/** {@inheritDoc} */
130130
@Override
131131
public Object[] toArray() {
132-
return this.delegate.keySet().toArray();
132+
return this.delegate.toArray();
133133
}
134134

135135
/** {@inheritDoc} */
136136
@Override
137137
public <T> T[] toArray(final T[] array) {
138-
return this.delegate.keySet().toArray(array);
138+
return this.delegate.toArray(array);
139139
}
140140

141141
/** {@inheritDoc} */
142142
@Override
143143
public boolean add(final E elem) {
144-
return (this.delegate.put(elem, Nothing.NOTHING) == null);
144+
return this.delegate.add(elem);
145145
}
146146

147147
/** {@inheritDoc} */
148148
@Override
149149
public boolean remove(final Object obj) {
150-
return (this.delegate.remove(obj) != null);
150+
return this.delegate.remove(obj);
151151
}
152152

153153
/** {@inheritDoc} */
154154
@Override
155155
public boolean containsAll(final Collection<?> collection) {
156-
return this.delegate.keySet().containsAll(collection);
156+
return this.delegate.containsAll(collection);
157157
}
158158

159159
/** {@inheritDoc} */
160160
@Override
161161
public boolean addAll(final Collection<? extends E> collection) {
162-
boolean changed = false;
163-
for (final E elem : collection) {
164-
changed |= add(elem);
165-
}
166-
return changed;
162+
return this.delegate.addAll(collection);
167163
}
168164

169165
/** {@inheritDoc} */
170166
@Override
171167
public boolean retainAll(final Collection<?> collection) {
172-
return this.delegate.keySet().retainAll(collection);
168+
return this.delegate.retainAll(collection);
173169
}
174170

175171
/** {@inheritDoc} */
176172
@Override
177173
public boolean removeAll(final Collection<?> collection) {
178-
return this.delegate.keySet().removeAll(collection);
174+
return this.delegate.removeAll(collection);
179175
}
180176

181177
/** {@inheritDoc} */

0 commit comments

Comments
 (0)