一、前言
基于alibaba的dubbo源码包中的com.alibaba.dubbo.common.utils.ConcurrentHashSet定义并发类集,继承java.util.AbstractSet并实现 java.util.Set接口,详情参见源码说明。
二、源码说明
package com.alibaba.dubbo.common.utils;@b@@b@import java.io.Serializable;@b@import java.util.AbstractSet;@b@import java.util.Iterator;@b@import java.util.Set;@b@import java.util.concurrent.ConcurrentHashMap;@b@@b@public class ConcurrentHashSet<E> extends AbstractSet<E>@b@ implements Set<E>, Serializable@b@{@b@ private static final long serialVersionUID = -8672117787651310382L;@b@ private static final Object PRESENT = new Object();@b@ private final ConcurrentHashMap<E, Object> map;@b@@b@ public ConcurrentHashSet()@b@ {@b@ this.map = new ConcurrentHashMap();@b@ }@b@@b@ public ConcurrentHashSet(int initialCapacity) {@b@ this.map = new ConcurrentHashMap(initialCapacity);@b@ }@b@@b@ public Iterator<E> iterator()@b@ {@b@ return this.map.keySet().iterator();@b@ }@b@@b@ public int size()@b@ {@b@ return this.map.size();@b@ }@b@@b@ public boolean isEmpty()@b@ {@b@ return this.map.isEmpty();@b@ }@b@@b@ public boolean contains(Object o)@b@ {@b@ return this.map.containsKey(o);@b@ }@b@@b@ public boolean add(E e)@b@ {@b@ return (this.map.put(e, PRESENT) == null);@b@ }@b@@b@ public boolean remove(Object o)@b@ {@b@ return (this.map.remove(o) == PRESENT);@b@ }@b@@b@ public void clear()@b@ {@b@ this.map.clear();@b@ }@b@}