File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Using Iterator for next and previous with TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Using Iterator for next and previous with TreeMap" Watch "Using Iterator for next and previous with TreeMap" New topic

Using Iterator for next and previous with TreeMap

Robert Davis

Joined: Dec 30, 2003
Posts: 14
Dear Ranchers:
I'm working on an assignment that uses a SortedMap implemented as a TreeMap to store a collection of objects. Part of the assignment is to use four buttons (first, previous, next and last) to retreive the objects from the collection. I have no problem with first and last but am having difficulty with both next and previous.
Here is the code for the ActionListener registered to the buttons with current being a Store object:

fillForm is just a method that extracts the variables in current and displays them in TextFields in the panel.
I know that at least part of my problem is with using Iterator. I expected the assignment for current with the next button ActionEvent to work. It does compile and run without any errors but does not result in any change to the value of current.
I know that my code for the previous button will not work (it does not compile) and I believe I understand correctly that this is because Iterator does not have a hasPrevious() or previous() method.
While ListIterator does have these methods, as far as I can figure out, ListIterator will not work with Maps, SortedMaps or TreeMaps.
I would appreciate any direction to resolve the next and previous problems.
[ February 26, 2004: Message edited by: Robert Davis ]
Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
Hi Bob,
I think to start with, the key to previous and next is of course - previous and next to what? The "current" object obviously. In other words at any given time you need to know where in the Map the user currently is and then return the object before or after that object in the Map. Is there any reason why you chose TreeMap? I think this would be easier with other classes maybe a LinkedList for example. In any event...
You're using a Collection class by calling keySet() on the TreeMap - specifically a Set implementation. The problem with Map classes vs List classes, is that their values are accessible via keys as opposed to via an index like say an ArrayList, so you have to jump through some hoops to determine where in the Map a given object is. So here's my suggestion.
1. Create an ArrayList based on stores.keySet()

2. figure out where in the array list the current object is and get the one before or after it

3. The you can go back into the Map and get the values if you need to based on the key.
Hope this at least starts you off in the right direction.
[ February 26, 2004: Message edited by: Brian R. Wainwright ]
Robert Davis

Joined: Dec 30, 2003
Posts: 14
Thank you for the prompt reply.
You are correct that there are other collection classes that make it easier to use next and previous. Unfortunately, in this case the homework assignment specifies TreeMap. The fortunate part is that means I have to learn how to make it work even if it isn't the easiest class for this process.
I had considered creating an array, but thought I might be missing something about Iterators.
I think your suggestion for ArrayList may be just the ticket.
I'll let you know how it works out.
Robert Davis

Joined: Dec 30, 2003
Posts: 14
Brian et al:
This is the code that does the job. As you can see I had to do some casting to get the right objects between the TreeMap collection and the ArrayList. I also created a int variable to hold the index value (both to shorten the length of the code and to try and make it more readable).

Thank you for the direction Brian.
If anyone can suggest improvements to my code for style or brevity, I would welcome them.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
That's a very good thing to want to learn more about.
Let's move this thread to the OO, Patterns, UML, and Refactoring forum, where they just love to talk about such topics...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Using Iterator for next and previous with TreeMap
It's not a secret anymore!