在Java中寻找ConcurrentHashSet吗?

方法一:Collections.newSetFromMap(new ConcurrentHashMap())

方法二:(引用自Apache Mina,位置org.apache.mina.util.ConcurrentHashSet)

package org.apache.mina.util; import java.util.Collection; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * A {@link ConcurrentHashMap}-backed {@link Set}. * * @author Apache MINA Project */ public class ConcurrentHashSet extends MapBackedSet { private static final long serialVersionUID = 8518578988740277828L; public ConcurrentHashSet() { super(new ConcurrentHashMap()); } public ConcurrentHashSet(Collection c) { super(new ConcurrentHashMap(), c); } @Override public boolean add(E o) { Boolean answer = ((ConcurrentMap) map).putIfAbsent(o, Boolean.TRUE); return answer == null; } }  

你可能感兴趣的:(Java)