我需要处理一个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)
在这个过程中,如果列表中的元素数量低于阈值(用户定义的正整数),则会被删除。同时,如果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);
List<List<Integer>> list2=MyMap.get(key2);
Boolean ret=false;
for(int i=0;i<list1.size();i++) {
List<Integer> sublist1=list1.get(i);
for(int j=0;j<list2.size();j++) {
List<Integer> sublist2=list2.get(j);
sublist1.removeAll(sublist2);
if(sublist1.isEmpty())
break;
}
if(sublist1.size()<=THRESHOLD)
list1.remove(sublist1);
if( list1.isEmpty()) {
MyMap.remove(key1);
ret=true;
}
}
return ret;
}
但程序出现错误:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification
at java.util.ArrayList$Itr.next
Map.iterator().remove()
是错误的,至少在一般情况下是这样。接口规定支持删除操作,但不支持添加操作。 - sharakan