• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

effect of overriding equals on Sets?  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: http://www.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
  • Quote
  • Report post to moderator
Thanks Raphael, for pointing out
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • 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.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!