• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

collections

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class test implements Comparator<test>
{
private int x;
test(int input) { x = input; }
public static void main( String args[] )
{
List list = new ArrayList();
list.add(new test(2));
list.add(new test(2));
Collections.sort(list);
}
public int compare( test t1 , test t2 )
{
return t1.x - t2.x;
}
}
The explanation for the above program.

This code will throw a ClassCastException. This version of Collections.sort() banks on the Comparable interface being implemented, not the Comparator interface. No comparator is passed to the sort() method.

I couldn't understand the above explanation.Please explain.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Collections class has 2 sort methods:
  • sort(List list)
  • sort(List list, Comparator c)

  • If you use the version with the Comparator, then that Comparator is used to sort the List. However, if you use the one that takes only a List, then it treats the List's elements as being Comparable (that is, implementing the Comparable interface).

    In this example, no Comparator is supplied to the sort method, and the elements are not Comparable.
    [ September 18, 2007: Message edited by: marc weber ]
     
    radhika ayirala
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I got it.Thanks.Can you please tell me,why elements are not comparable.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by radhika ayirala:
    I got it.Thanks.Can you please tell me,why elements are not comparable.


    When we say an object is "Comparable," we mean it implements the Comparable interface, overriding its compareTo method. So it literally IS-A Comparable. The sort method must try to upcast to type Comparable, which is why it throws a ClassCastException if the elements are not Comparable.

    In this example, the class implements Comparator, but not Comparable.
     
    radhika ayirala
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks
     
    Enjoy the full beauty of the english language. Embedded in this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic