• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding comparator interface

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,
Actually to sort elements in the ArrayList i have used Collections.sort(ob);
and Collections.sort(ob,new OwnComparator());

Here in the class OwnComparator i have implemented the interface comparator which contains two methods i.e., compare and equals methods.

If i didn't overrite equals method why its not raising any error.
please suggest your ideas.

import java.util.*;
class sortonarraylist
{
public static void main(String[] args)
{
ArrayList ob=new ArrayList();
ob.add(10);
ob.add(11);
ob.add(9);
ob.add(5);
System.out.println("Hello World!"+ob);
Collections.sort(ob);
System.out.println("Hello World! sort order"+ob);
Collections.sort(ob,new OwnComparator());
System.out.println("Hello World! our own sort order"+ob);
}
}
import java.util.*;
class OwnComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
return o1.toString().compareTo(o2.toString());

}
/*public boolean equals(Object o){
System.out.println("in equals");
return true;
}*/

};
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OwnComparator is an object, so it extends java.lang.Object implicitly, which is where it gets an implementation of the equals method. Of course, Object.equals knows nothing about what OwnComparator does, so it may do the wrong thing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic