Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

generics...

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>())
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried ...Set<A> cs=new TreeSet<A>(new A<A>()) and it gives warning.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with the new TreeSet(new A<A>()) statement it is not giving me any warnings...
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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()?
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that code too gives warning...
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
 
reply
    Bookmark Topic Watch Topic
  • New Topic