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();
}
}
}
TreeMap
has a descendingMap
method (note that it returns a view on the original map, it does not copy it):
//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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With