Имам HashMap, той е от типа HashMap<String,HashMap<String,int>>
сега трябва да повторя този HashMap и да изтрия вътрешните HashMap, които имат стойност 0 за всеки ключ.
Ако такова премахване направи вътрешната HashMap празна, тогава съответният ключ на вътрешната HashMap се премахва от външната HashMap. Опитах да повторя над него и след това да премахна елементи, които отговарят на изискванията, но това ми хвърля ConcurrentModificationException
.
Опитах следния код:
synchronized(MyConstants.cliListUpdateList)
{
synchronized(MyConstants.cliList)
{
outerEntries = MyConstants.cliListUpdateList.entrySet();
outerIterator = outerEntries.iterator();
while(outerIterator.hasNext())
{
outerEnt = (Entry) outerIterator.next();
innerHashMap = (HashMap) outerEnt.getValue();
synchronized(innerHashMap)
{//synchronize innerhashmap
innerEntries = innerHashMap.entrySet();
innerIterator = innerEntries.iterator();
synchronized(innerIterator)
{
while(innerIterator.hasNext())
{
innerEnt = (Entry) innerIterator.next();
int k = Integer.parseInt((String)innerEnt.getValue());
if(k==0)
{
innerHashMap.remove(innerEnt.getKey());
if(innerHashMap.isEmpty())
{
MyConstants.cliListUpdateList.remove(outerEnt.getKey());
}
ArrayList ports = (ArrayList) MyConstants.cliList.get(outerEnt.getKey());
ports.remove((String)innerEnt.getKey());
if(ports.isEmpty())
{
MyConstants.cliList.remove(outerEnt.getKey());
}
}
else
{
k--;
innerHashMap.put(innerEnt.getKey(), k+"");
MyConstants.cliListUpdateList.put(outerEnt.getKey(), innerHashMap);
}
}
}
}//synchronize innerhashmap
}
System.out.println(MyConstants.cliListUpdateList + " <---> "+ MyConstants.cliList);
}
}
Получавам изключението на този ред: innerEnt = (Entry) innerIterator.next();
. Опитах метода за премахване, предоставен от класа Iterator. Но това също не е добре.
РЕДАКТИРАНЕ
от документите на Java знам толкова if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this(ConcurrentModificationException) exception
, но имам нужда от точно същата функционалност.
HashMap
. - person David Harkness   schedule 12.03.2012