| Author |
generics...
|
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
Hi , I got it from ExamLab practice exam 1. import java.util.*; class A implements Comparator{ public int compare(Object ob1,Object ob2){ return 1; } public String toString(){ return "W"; } } public class TMap{ public static void main(String argv[]){ Set<A> cs=new TreeSet<A>(new A()); cs.add(new A()); cs.add(new A()); cs.add(new A()); for(A a:cs) System.out.print(a+","); } } the above program compiles with warning but everything in this program are typesafe.then why it's giving warning? is anything wrong with comparator implementation? if i change compare method ... public int compare(Object ob1,Object o2) { return ob1.compareTo(ob2); } ...it's giving compiler error. please help me out.... Thanks Preetha
|
 |
M Srilatha
Ranch Hand
Joined: Aug 27, 2008
Posts: 137
|
|
hi, The reason for compiler warnings is Comparator interface is a parameterized one. And in your code, you are not passing any type parameter while implementing. Please have a look at the Java API for more info about Comparator interface! The above code doesn't compile! Because Object class doesnt implement Comparator interface and doesnt have a method compareTo(), ob1.compareTo(ob2) will give compiler error! Hope this is clear! [ December 01, 2008: Message edited by: M Srilatha ]
|
Thanks,<br />Srilatha M
|
 |
Vipun Reddy
Greenhorn
Joined: Mar 19, 2008
Posts: 23
|
|
Hi Preetha, Changing your code to something like this.. will not throw any warnings.. Reason : You say that TreeSet takes in a Generic type of class A . But when implementig comparator interface on class A you should also pass on the generic type on it.. check out the api doc for java.util.Comparator Interface for further reference.. So in the code above implemented abstract method takes the generic type as parameters and throws no compiler warnings. [ December 01, 2008: Message edited by: Vipun Reddy ]
|
Vipun Reddy
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
Hi Vipun, I tried your code it gives warning. anyway i will go through documentation. warning on...Set<A> cs=new TreeSet<A>(new A());//unchecked conversion. Thanks Preetha
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
You are creating a TreeMap where the comparator is the class itself whose objects are being added. I think you should use the statement Set<A> cs=new TreeSet<A>(new A()) as Set<A> cs=new TreeSet<A>(new A<A>())
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
|
I tried ...Set<A> cs=new TreeSet<A>(new A<A>()) and it gives warning.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
with the new TreeSet(new A<A>()) statement it is not giving me any warnings...
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Object class does not have compareTo() method in it. Does class A have any member that could be used to compare two objects equality using compareTo()?
|
cmbhatt
|
 |
Preethi Dev
Ranch Hand
Joined: Sep 07, 2008
Posts: 265
|
|
|
that code too gives warning...
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Originally posted by Preetha Arun: Hi Vipun, I tried your code it gives warning. anyway i will go through documentation. warning on...Set<A> cs=new TreeSet<A>(new A());//unchecked conversion. Thanks Preetha
Are you implementing the Comparator interface parameterized with class A for example with following systax: Now Set<A> set = new TreeSet<A>(new A()); should not give you and warning. Because one of TreeSet constructors accepts Comparator that you are providing correctly;
|
 |
 |
|
|
subject: generics...
|
|
|