aspose file tools*
The moose likes Beginning Java and the fly likes Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterator" Watch "Iterator" New topic
Author

Iterator

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
My understanding is that the Iterator is a class and the iterator method is of the TreeSet collection which returns an iterator object that is referenced by the a variable say recSet

i.e. to be using TreeSet:

Set tset = new TreeSet( )
I would have the following line of code to display a list using iterator:

Iterator recSet = tset.iterator( );

while (recSet.hasNext( ) )
System.out.println( (String)recSet.next( ) );

However, I am using TreeMap as I needed to store key and value pairs where the key needs to be sorted.

This is where I am getting confused. One of the constrains is we have to declare an Iterator method i.e.

public Iterator getIterator( )

This is where I'm getting confused a little And struggling to find somewhere as an example, where it has been used in a TreeMap. I couldn't find anything in the API when looking under TreeMap.

Is someone able to assist me with explaining how I would implement this in a TreeMap or alternatively point me in the right direction?
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Do you want to iterate over the keys in the map? If that's what you want, you may want to check if there are any methods in the TreeMap interface that allow you to get the set of keys.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think Warren is suggesting that you use TreeMap methods to obtain a Set of your TreeMap keys (or perhaps a Collection of its values), then get an iterator for that Collection.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I think it's far more likely that she wants to iterate the key/value pairs. Maybe there's a method in Map that returns a Set of the pair entries...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by David Harkness:
...Maybe there's a method in Map that returns a Set of the pair entries...

Hmmm, I missed that one. Very interesting...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterator