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 generics... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics..." Watch "generics..." New topic
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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...
 
Similar Threads
Comparator interface
compilation warnings
About object ,reference and class variable
Comparator and TreeSet
Sets