• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

compilation warnings

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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




 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks michael
i got it .
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic