http://momsbaby1986.iteye.com/blog/1452022
java没有提供ConcurrentHashSet, 可以通过ConcurrentHashMap来间接实现
- import java.util.AbstractSet;
- import java.util.ConcurrentModificationException;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.concurrent.ConcurrentHashMap;
- public class ConcurrentHashSet<E> extends AbstractSet<E> implements Set<E>, java.io.Serializable {
- private static final long serialVersionUID = -8672117787651310382L;
- private static final Object PRESENT = new Object();
- private final ConcurrentHashMap<E, Object> map;
- public ConcurrentHashSet(){
- map = new ConcurrentHashMap<E, Object>();
- }
- public ConcurrentHashSet(int initialCapacity){
- map = new ConcurrentHashMap<E, Object>(initialCapacity);
- }
- public Iterator<E> iterator() {
- return map.keySet().iterator();
- }
- public int size() {
- return map.size();
- }
- public boolean isEmpty() {
- return map.isEmpty();
- }
- public boolean contains(Object o) {
- return map.containsKey(o);
- }
- 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();
- }