• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Iterator for next and previous with TreeMap

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
TIA
Bob
[ February 26, 2004: Message edited by: Robert Davis ]
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
---BW
[ February 26, 2004: Message edited by: Brian R. Wainwright ]
 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian:
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.
Thanks
Bob
 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
TIA
Bob
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
[ February 28, 2004: Message edited by: Dirk Schreckmann ]
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic