• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam LAb Question ON Collections

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am unable to understand the Line as given in explanation of Exam Lab

Tree cast to Comparable
11.PNG
[Thumbnail for 11.PNG]
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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





 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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....

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic