File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam LAb Question ON Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam LAb Question ON Collections" Watch "Exam LAb Question ON Collections" New topic
Author

Exam LAb Question ON Collections

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i am unable to understand the Line as given in explanation of Exam Lab

Tree cast to Comparable


[Thumbnail for 11.PNG]



OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

When you store the instances of the particular class, in your case, Test class., should implements the Comparable interface. The TreeSet uses this method to sort the instances which you've stored in it. Since, the Comparable is a interface, the compiler can't check it in the compile time!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

Whenever we are sorting some objects using some specific technique
we have to implement the comparable interface and hence we have to implement the
compareTo(Object o) method

this method gets called when we call
Collection.sort();


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

i have done this exam today only,whenever we are talking about the treeset(navigalbe set) ,the elements in it we are adding must be mutually comparable and the type parameter we specify with that must implement comparable interface,else ClassCastException(unchecked)will be thrown.
as test class doesn't implement comparable interface,so program suffers from exceptions at runtime.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

I understand that Test needs to implement comparable in order to be sorted but i am unable to understand the last line of Explanation as given in Exam lab

[b]when the class TreeSet attempts to cast Test into Comparable ,a ClassCastException would be Produced[/b]

The Text in BOLD

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

mohitkumar gupta wrote:unable to understand the last line of Explanation as given in Exam lab
when the class TreeSet attempts to cast Test into Comparable,a ClassCastException would be Produced

because you are using generics compiler is making sure that all elements you are adding in the treeset must be mutually comparable.
when it saw that test doesn't implements comparable,so it's attempts to cast fails,and an exception will be thrown.
if you post the code here,will help you able to understand more.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:
because you are using generics compiler ......

No, no need of generics here. Legacy system also will give a ClassCastException. Because, When the TreeSet uses the interface when it's sorting!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Abimaran Kugathasan wrote:
no need of generics here. Legacy system also will give a ClassCastException. Because, When the TreeSet uses the interface when it's sorting!

yes you are right,i forgot to edit that,thanks for correction.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

When you add any elements into the TreeSet all the elements should be mutually comparable..
If you are not using generic type..then you can add any type elements into the Treeset but while sorting you will get ClassCastException..
please check below code






SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@mohit...

I know this is a difficult concept but Concentrate more on the Generics and Collection because you will get atleast 5 question from thisr....

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Exam LAb Question ON Collections
 
Similar Threads
the free sample practice exam from pearsonvu
How long does it take to prepare SCJP
Reference Type Casting
Reference Variables and Inheritance
Passed SCJP Exam (was Taking same)