• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another question from mock exam

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
tm.add("Bill");
tm.add("Kathy");
tm.add("Carl");
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody clear this?Should I assume that TreeMap doesn't sort elements in ascending order?
Thanks
Veena
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
I find it's more convenient to have this downloaded onto my disk. You can do that here:download
You may have to register on the site, which is also a good thing to do.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
I find it's more convenient to have this downloaded onto my disk. You can do that here:download
You may have to register on the site, which is also a good thing to do.

Steve,
I already use Api alot.Thanks for reffering.But after ur sincere advise I read with more alertness...it states
public class TreeMap
extends AbstractMap
implements SortedMap, Cloneable, Serializable
Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.
I misunderstood ascending key order as ascending order for elements.Now I know the difference between TreeSet & TreeMap.Thanks alot and I am sorry for bothering without digging into the API.
Veena
[ September 14, 2003: Message edited by: Veena Point ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic