4

This is my map with iterators to remove some data based on some conditions, now I want to run the same map in reverse direction and conditions accordingly.

But I couldn't find any ListIterator to go back easily.

How Can I go about this ?

Also the Map implementation I am using is a TreeMap

for(int i=countIteration;i<(countIteration+2);i++)
{
    Iterator it = imageFilexxSm.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        if(pairs.getKey().equals(data1.get(i).replace(".png", ".mp3")))  
        {
            it.remove();
        }
    }

    Iterator itr = imageFilexxS.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry pairsx = (Map.Entry)itr.next();
        if(pairsx.getKey().equals(data1.get(i)))     
        {
            System.out.println("entry deleted."+pairsx.getKey());
            itr.remove();
        }
    }
}
4

1 回答 1

18

TreeMap有一个descendingMap方法(注意它返回原始地图上的视图,它不会复制它):

//iterate over the entry set in reverse order
for (Map.Entry<X,Y> e : map.descendingMap().entrySet()) {
    X key = e.getKey();
    Y value = e.getValue();
}
于 2013-05-14T13:13:43.003 回答