This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Another question from mock exam

Veena Pointi
Ranch Hand
Posts: 442
In Kathy & Berts book it is given elements are displayed in ascending order for TreeSet & TreeMap,ex is in pno425....Please go through the following codes

Prints BillKathyCarl
Doesn't TreeMap prints elements in ascending order,coz it is sorted by natural order or custom comparison rules?i.e same as TreeSet.
To verify I tried the following code
[code]
import java.util.*;
public class Thirsk1{
public static void main(String argv[]){
Thirsk t = new Thirsk();
t.a19();
}
public void a19(){
TreeSet tm = new TreeSet();
Iterator it = tm.iterator();
while(it.hasNext()){
System.out.print(it.next());
}
}
}
[/code
Even above code prints the same.
But in Kathy & Berts book it is given elements are displayed in ascending order for TreeSet & TreeMap,ex is in pno425
But both the above codes are showing TreeSet & TreeMap print elements by insertion order.Does this mean K&B has given wrong?
Thanks
Veena

Bert Bates
author
Sheriff
Posts: 8898
5
Veena -
I noticed that your second program is called Thirsk1, but that it is still making an instance of Thirsk. What happens if you make an instance of Thirsk1 in your second program?
-Bert

Veena Pointi
Ranch Hand
Posts: 442
Oh!Thats right.I am sorry.But I thought even TreeMap prints in ascending ordere,coz both TreeSet TreeMap are sorted by natural order or custom comparison rules.Only difference between them would be TreeMap uses key value pair where as TreeSet doesnt......Am I right?If this is the case why TreeMap prints billkathycarl,why not billcarlkathy,why only TreeMap is sorting the elements in ascending order?
veena
[ September 14, 2003: Message edited by: Veena Point ]

Steve Lovelace
Ranch Hand
Posts: 125
And the fact that TreeMap uses key and value means it could sort by one or the other. So the question is, which one?

Veena Pointi
Ranch Hand
Posts: 442
Can anybody clear this?Should I assume that TreeMap doesn't sort elements in ascending order?
Thanks
Veena

Steve Lovelace
Ranch Hand
Posts: 125
Rather than assume anything, I would look at the Java API docs entry for TreeMap. If you don't know about the docs, or aren't in the habit of using them, then IMHO it would be a very good thing to develop this habit. Here is the link:J2SE 1.4.2 API
You may have to register on the site, which is also a good thing to do.

Veena Pointi
Ranch Hand
Posts: 442
Originally posted by Steve Lovelace:
Rather than assume anything, I would look at the Java API docs entry for TreeMap. If you don't know about the docs, or aren't in the habit of using them, then IMHO it would be a very good thing to develop this habit. Here is the link:J2SE 1.4.2 API
You may have to register on the site, which is also a good thing to do.

Steve,