小编Kau*_*aur的帖子

如何在java中将两个迭代器保留在map上,并在没有ConcurrentModificationException的情况下删除其间的键

我必须处理一个 Map <BitSet,List<List<Integer>> MyMap

if (key1 contains all of corresponding true bits of key2)
     Remove from key2 all those values which are common with key1)
Run Code Online (Sandbox Code Playgroud)

在此过程中,如果列表中的元素数量低于THRESHOLD(用户定义的正整数),则将其删除.此外,如果Map包含空列表,则删除相应的键.

我使用以下代码:

List<BitSet> keys = new ArrayList<>(MyMap.keySet());  
ListIterator it1=keys.listIterator();
while(it1.hasNext())  {
     BitSet key1=(BitSet)it1.next();
     ListIterator it2=keys.listIterator(it1.nextIndex());
     while(it2.hasNext()) {
         BitSet key2=(BitSet)it2.next();                 
         BitSet ankey=(BitSet)key1.clone();
         ankey.and(key2);    
         if(ankey.equals(key1)) {//key1 is subset and key2 is superset
               if(removePoints(key1,key2))  {
                     it1.remove();
                     break;
               }
         }
         else if(ankey.equals(key2))  {                           
              if(removePoints(key2,key1))  {
                    it2.remove();                         
              }
         }
     }
}

public static boolean removePoints(BitSet key1,BitSet key2)
 {
     List<List<Integer>> list1=MyMap.get(key1); …
Run Code Online (Sandbox Code Playgroud)

java iterator hashmap concurrentmodification

7
推荐指数
1
解决办法
1579
查看次数

如何为BitSet类型的元素创建SortedSet(例如TreeSet)

我有许多(power(2,k))BitSet对象,我想将它们存储在一个SortedSet.我用的代码是:

Set <BitSet> S= new TreeSet<>();
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误: java.lang.ClassCastException: java.util.BitSet cannot be cast to java.lang.Comparable

如何实现类似的界面?或者有没有其他方法来排序这些类型的元素BitSet

java comparable classcastexception bitset

5
推荐指数
1
解决办法
4万
查看次数