`
momsbaby1986
  • 浏览: 33948 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ConcurrentHashSet简单实现

    博客分类:
  • Java
 
阅读更多

 

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();
	}

 

 

通过ConcurrentHashMap来间接实现

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics