Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

when and how is compareTo() called for each element

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//PLEASE EXPLAIN TO ME WHICH IS THE CALLED OBJECT AND WHICH IS THE CALLING OBJECT ,WHY

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Song2.compareTo method is not at all called in this case. Because Song2 objects are added to ArrayList, which is not 'sorted' and hence doesn't need compareTo method implementation. However if Song2 objects were being added to a sorted collection such as TreeSet, then compareTo would have been called. In that case 'caller' will be TreeSet object, and called on Song2 objects.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Song2.compareTo method is not at all called in this case. Because Song2 objects are added to ArrayList, which is not 'sorted' and hence doesn't need compareTo method implementation. However if Song2 objects were being added to a sorted collection such as TreeSet, then compareTo would have been called. In that case 'caller' will be TreeSet object, and called on Song2 objects.



I m afraid. compareTo () method is called when Collections.sort() is called .



Here ArrayList contains Song2 objects which implements comparable interface. Collections.sort() will actually compareTo() method to sort it .
 
saurabhthard aggarwal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to K&B SCJP 6 Collections.sort() method will call compareTo() of comparable interface.
Input of the program is unsorted.

Output of the program is sorted.

Do you have any idea of how compareTo() is used inside sort() method.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Did you had look at API ? Collection sort() . Check it out
 
Byju Joy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collections.sort is not the only place which can trigger compareTo call. In the code below compareTo is called without Collections.sort. In a sorted collection while trying to insert a new item a suitable place is to determined by comparing the new item with existing items.

 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic