Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

effect of overriding equals on Sets?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the effect of overrding equals method on Set.

I mean,
Can a set have objects which are different but are "meaningfully" equal. The equals() has been correctly overridden i n the custom class to test the "meaningful equality"

Thanks in advance
[ July 23, 2008: Message edited by: Deepak Bajoria ]
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Bajoria:
What is the effect of overrding equals method on Set.

I mean,
Can a set have objects which are different but are "meaningfully" equal. The equals() has been correctly overridden i n the custom class to test the "meaningful equality"

Thanks in advance



Nope, javadoc says...

boolean add(E e)

Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.

 
Deepak Bajoria
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But see here this code:

This produces the output:
Size:1
Size:2

Two objects here obj and obj1 are meaningfully equal, But they have been added to the set.
[ July 23, 2008: Message edited by: Deepak Bajoria ]
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Bajoria:
But see here this code:

This produces the output:
Size:1
Size:2

Two objects here obj and obj1 are meaningfully equal, But they have been added to the set.

[ July 23, 2008: Message edited by: Deepak Bajoria ]



When you use anything based on Hash you should override the method hashCode respecting the contract between hashCode and equals. Study them.

Take a look at this topic too, here I explained a bit: https://coderanch.com/t/269571/java-programmer-SCJP/certification/Do-have-override-equals-due
 
Deepak Bajoria
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raphael, for pointing out
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class AB
{
int i;
public boolean equals(Object o){
if(!(o instanceof AB)) return false;
return ((AB)o).i==i;
}
@Override
public int hashCode() {
return i;
}

AB(int i){this.i=i;}
}
public class demo5
{
public static void main(String []args){
HashSet<AB> s=new HashSet<AB>();
AB obj1=new AB(6);
s.add(obj1);

System.out.println("Size:"+s.size());
AB obj=new AB(6);
s.add(obj);
System.out.println("Size:"+s.size());
}
}

Now your program will work as you want.
 
reply
    Bookmark Topic Watch Topic
  • New Topic