Get the previous element of a LinkedHashMap

Viewed 2553

I need to get the previous element of a LinkedHashMap. I tried using the ListIterator because it has the previous() method. But the problem is ListIterator needs a List not a set.

ListIterator<Entry<String, Integer>> it = (ListIterator<Entry<String, Integer>>) unitsItems.entrySet().iterator();

I have to transform my entrySet into a list. So I tried this :

List entryList= new ArrayList (unitsItems.entrySet());
     ListIterator<Entry<String, Integer>> it = (ListIterator<Entry<String, Integer>>) entryList.iterator();

I got this error:

java.util.ArrayList$Itr cannot be cast to java.util.ListIterator

Can anyone tell me the correct way to transform the set to a list and then use it in ListIterator? Thank you.

3 Answers
Related