Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compilation warnings

 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi ,
1)can anybody please explain me why it gives a warning while compilation?
2)what should be done to avoid this warning?
3)in which all conditions compilation warnings gives in generics ?

the below code is compiled in j2se 1.6 environment.

thanks in advance




 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Warning because that is not an error but may cause problems.
2) Those warning should be removed as far as possible.
3) For generics warnings comes when you use a parametrized thing(class,interface , method) without specifying the parameter like you have done for the Comparator.

The best way to handle warnings is to use an IDE like eclipse. It will suggest you how you can remove those warnings.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparator is generic - it should be Comparator<ABICOMP>.

Instead of making ABICOMP implement Comparator<ABICOMP> (meaning one instance can compare any two other instances), you should consider implementing Comparable<ABICOMP> instead. This allows each ABICOMP instance to compare itself to any other instance. You will then no longer need to create an instance just to use as the Comparator. The tree set can be created as "new TreeSet<ABICOMP>()" and will let each element compare itself to the other elements.
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all.

i tried with implementing implement Comparable <ABICOMP> it worked fine (no warnings).But,implementing Comparator<ABICOMP> does not work out ..
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AshutoshP Patil wrote:But,implementing Comparator<ABICOMP> does not work out ..

If you want more help with that, then please explain exactly what you mean - TellTheDetails.
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to sort the objects of my class ABICOMP and ptint myclass objects by toString() method
by two ways such that no warnings should be there
1)one using comparator
2)second using comparable
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AshutoshP Patil wrote:thanks all.

i tried with implementing implement Comparable <ABICOMP> it worked fine (no warnings).But,implementing Comparator<ABICOMP> does not work out ..


Comparable is the right interface to use in your situation. Comparable is used on the classes that are being sorted.

Comparator is used if you want Java to ignore the rules that Comparable defines (or if the class does not implement Comparable). For example:
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks michael
i got it .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic