在Java中寻找ConcurrentHashSet吗? - diwayou的专栏 - 博客频道 - CSDN.NET


在Java中寻找ConcurrentHashSet吗?


分类:
Java


269人阅读
评论(0)
收藏
举报

 

方法一:Collections.newSetFromMap(new ConcurrentHashMap<Object,Boolean>())

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

 

  1. package org.apache.mina.util;  
  2. import java.util.Collection;  
  3. import java.util.Set;  
  4. import java.util.concurrent.ConcurrentHashMap;  
  5. import java.util.concurrent.ConcurrentMap;  
  6. /** 
  7.  * A {@link ConcurrentHashMap}-backed {@link Set}. 
  8.  * 
  9.  * @author <a href="http://mina.apache.org" mce_href="http://mina.apache.org">Apache MINA Project</a> 
  10.  */  
  11. public class ConcurrentHashSet<E> extends MapBackedSet<E> {  
  12.     private static final long serialVersionUID = 8518578988740277828L;  
  13.     public ConcurrentHashSet() {  
  14.         super(new ConcurrentHashMap<E, Boolean>());  
  15.     }  
  16.     public ConcurrentHashSet(Collection<E> c) {  
  17.         super(new ConcurrentHashMap<E, Boolean>(), c);  
  18.     }  
  19.     @Override  
  20.     public boolean add(E o) {  
  21.         Boolean answer = ((ConcurrentMap<E, Boolean>) map).putIfAbsent(o, Boolean.TRUE);  
  22.         return answer == null;  
  23.     }  
  24. }  

 

相关文章:

  • 2021-12-08
  • 2022-01-20
  • 2021-09-06
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2021-07-16
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案