Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
HDFS, mail # user - Re: CompareTo vs equals


Copy link to this message
-
Re: CompareTo vs equals
Ted Yu 2013-03-28, 03:22
>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 <[EMAIL PROTECTED]> 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
>
>
>