aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another question from mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another question from mock exam" Watch "Another question from mock exam" New topic
Author

Another question from mock exam

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
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();
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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
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

Joined: Sep 03, 2003
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?


The Inner that is named is not the true Inner.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Can anybody clear this?Should I assume that TreeMap doesn't sort elements in ascending order?
Thanks
Veena
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
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
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

Joined: Jun 20, 2002
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
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 ]
 
Consider Paul's rocket mass heater.
 
subject: Another question from mock exam