Return-Path: X-Original-To: apmail-hadoop-user-archive@minotaur.apache.org Delivered-To: apmail-hadoop-user-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 08FF3F6F0 for ; Thu, 28 Mar 2013 03:22:40 +0000 (UTC) Received: (qmail 80291 invoked by uid 500); 28 Mar 2013 03:22:34 -0000 Delivered-To: apmail-hadoop-user-archive@hadoop.apache.org Received: (qmail 80068 invoked by uid 500); 28 Mar 2013 03:22:34 -0000 Mailing-List: contact user-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@hadoop.apache.org Delivered-To: mailing list user@hadoop.apache.org Received: (qmail 79580 invoked by uid 99); 28 Mar 2013 03:22:33 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Mar 2013 03:22:33 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of yuzhihong@gmail.com designates 209.85.215.46 as permitted sender) Received: from [209.85.215.46] (HELO mail-la0-f46.google.com) (209.85.215.46) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Mar 2013 03:22:29 +0000 Received: by mail-la0-f46.google.com with SMTP id fq12so16631951lab.5 for ; Wed, 27 Mar 2013 20:22:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=YxSTPAAZMlGn51yl2d+ZZWMQFoFgxjgHWo8VyoFqwmQ=; b=M7rf+e2TBdE920E4T2K7v+IrSB+VayMepEhPvuZexfowD76+u1kpNuYcGsjhTC29s8 6p77MTUgEMFeUIQcc6Na7R1vCDAk975wV5ikDUUq3yDv3i3pQMOEbeaHiodfk1CORf0q dNKPj8/RwYvOC8l23QuSAhAwL1Bga5Umenxyl4XeLscQmtyY09pjmbCDym/8MNHZAPZ2 nGd6yGXsLkjpbQ/32kPQkQB8QMLi5ThlHdykN6lHw/fEpzCBWgD/q1gtBL+zUX+9zABH rpR3/1OpjSr07P2pRcAGYcPHqrGSDuOH4bCMlQlxvN7b+NYRMAmL6MiVV1tQT8kCQzFL 2FVA== MIME-Version: 1.0 X-Received: by 10.112.5.138 with SMTP id s10mr11434680lbs.73.1364440928270; Wed, 27 Mar 2013 20:22:08 -0700 (PDT) Received: by 10.112.84.133 with HTTP; Wed, 27 Mar 2013 20:22:08 -0700 (PDT) In-Reply-To: <1364440561.95550.YahooMailNeo@web194706.mail.sg3.yahoo.com> References: <1364382700.45468.YahooMailNeo@web194702.mail.sg3.yahoo.com> <1364440561.95550.YahooMailNeo@web194706.mail.sg3.yahoo.com> Date: Wed, 27 Mar 2013 20:22:08 -0700 Message-ID: Subject: Re: CompareTo vs equals From: Ted Yu To: user@hadoop.apache.org, Sai Sai Content-Type: multipart/alternative; boundary=14dae94ed821d8c03404d8f3a80b X-Virus-Checked: Checked by ClamAV on apache.org --14dae94ed821d8c03404d8f3a80b Content-Type: text/plain; charset=ISO-8859-1 >From http://www.javapractices.com/topic/TopicAction.do?Id=10 : consistency with equals is required for ensuring sorted collections (such as TreeSet) are well-behaved. On Wed, Mar 27, 2013 at 8:16 PM, Sai Sai wrote: > IntPair class has these 2 methods, i understand that compareTo is used > for comparing but when is equals method used and is it necessary to write > it when we alread have implemented compareTo method. > > @Override > public int compareTo(IntPair that) { > int cmp = first.compareTo(that.first); > if(cmp==0){ > cmp = second.compareTo(that.second); > } > return cmp; > } > @Override > public boolean equals(Object obj) { > if (obj instanceof IntPair){ > IntPair that = (IntPair)obj; > return (first.equals(that.first) && second.equals(that.second)); > } > return false; > } > > Thanks > Sai > > > --14dae94ed821d8c03404d8f3a80b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable From=A0http://www.javapractices.com/topic/TopicAction.do?Id=3D10 :

consistency with=A0equals=A0is required for ensuring sorted coll= ections (such as=A0TreeSet) a= re well-behaved.

On Wed, Mar 27, 2013 at 8:16 PM, Sai Sai <s= aigraph@yahoo.in> wrote:
IntPair =A0class has these 2 methods, i understand= that compareTo is used for comparing but when is equals method used and is= it necessary to write it when we alread have implemented compareTo method.=

=
public int= compareTo(IntPair that) {
= int cmp =3D first.compareTo(that.first);
if(cmp=3D=3D0){
cmp =3D second.compareT= o(that.second);
}
return cmp;
}
@Override
public boolean equals(Object obj) {=
if (obj instanceof IntPair){
IntPair that =3D (IntPa= ir)obj;
return (fi= rst.equals(that.first) && second.equals(that.second));
}
=
return false;
= }

Thanks
Sai


=
--14dae94ed821d8c03404d8f3a80b--