Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2006, 03:03 PM
Authorized User
 
Join Date: Dec 2005
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 ............
 
Old January 16th, 2006, 04:49 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
http://www.jonemerson.net/





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 12: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 11:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.