Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old January 5th, 2006, 03:03 PM
Authorized User
Join Date: Dec 2005
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Doubt about Comparator interface implementation

When u implement a interface u should implement all it's methods.

But when we implement Comparator interface u can leave the equals()

method . where this method is implemented? (A Interface can't

implement it's method ...)

       please Help me ............
Reply With Quote
  #2 (permalink)  
Old January 16th, 2006, 04:49 AM
Friend of Wrox
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts

Excellent question, Pandian!  The Comparator.compare(Object, Object) and Comparator.equals(Object) methods are extremely different.  Comparator.compare(Object, Object) specifies the meat of the Comparator interface: it determines if the first parameter is less than, equal to, or greater to the second.  Comparator.equals(Object) instead compares a different Comparator to the current one.  

By default, an Object implementing the Comparator interface will inherit the equals(Object) method from its base class, Object.  Object.equals(Object) returns true if the current object occupies the same memory space as the passed object.  Basically, it only returns true if itself is passed to it.  

The Comparator interface essentially maps out an option for equals(Object) to additionally return true if the passed Comparator has the same Comparator.compare(Object, Object).  That is, they'll both sort a List in exactly the same way.  The interface specifies this option to allow applications especially interested in performance a way to cache Comparators so they need not be created for each use.

As a beginner, you shouldn't worry about overriding equals(Object).  Even as an advanced Java developer, you will likely only need to implement compare(Object, Object), as this is what Collections.sort(List) and other collections methods care about.

More information: http://java.sun.com/j2se/1.4.2/docs/...omparator.html

Jon Emerson
Adobe Systems, Inc.
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubt about ServletRequest Interface pandian Servlets 1 January 16th, 2006 12:31 AM
SSL implementation piyush_vish ASP.NET 1.0 and 1.1 Professional 0 January 6th, 2006 07:30 AM
c# implementation help cshanmuganathan ASP.NET 1.0 and 1.1 Basics 1 May 28th, 2005 01:08 AM
Comparator with Strings elisabeth Java Espanol 1 December 20th, 2004 04:47 PM
Implementation Decision reckon ASP.NET 1.x and 2.0 Application Design 2 June 28th, 2004 12:04 PM

All times are GMT -4. The time now is 09:07 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.