• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused with Set equality

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class CollTest1
{

public static void main(String[] args)
{
SetElem k1 = new SetElem(1);
SetElem k2 = new SetElem(1);
Set s = new HashSet();
System.out.println(k1.equals(k2)); // #1 true
System.out.println(s.add(k1)); // #2 true
System.out.println(s.add(k2)); // #3 true
System.out.println(s.add(new String("raju"))); //true
System.out.println(s.add(new String("raju"))); //false
System.out.println(s.size());
}
}
class SetElem
{
int i;
public SetElem(int i){
this.i = i;
}
public boolean equals(Object o){
return true;
// return ((SetElem)o).i == this.i;
}
public int hasCode(){
return 1;
// return i;
}
};
how #3 return true in above example? please explain.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have any hashCode method. If you think you have one, check its spelling carefully.
 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, You are right
 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right.
One more question regarding this. Please calrify

import java.util.*;
class CollTest1
{

public static void main(String[] args)
{
SetElem k1 = new SetElem(1);
SetElem k2 = new SetElem(2);
Set s = new HashSet();
s.add(k1);
s.add(k2);
System.out.println(s.size()); // #1 print 2
k2.i = 1;
System.out.println(s.size()); // #2 print 2
s.remove(k2);
System.out.println(s.size()); // #3 print 1

}
}
class SetElem
{
int i;
public SetElem(int i){
this.i = i;
}
public boolean equals(Object o){
return ((SetElem)o).i == this.i;
}
public int hashCode(){
return i;
}
};

How line 3 prints value 1?, whenever an object is added to a set, does set will create a copy of a object and always make reference to that?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkata Saraswathi wrote: whenever an object is added to a set, does set will create a copy of a object and always make reference to that?



First, use code tag to post your Code. Coming to your Question . Java Class method argument always pass by value[copy of a object ] .
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

seetharaman venkatasamy wrote:Java Class method argument always pass by value[copy of a object ] .


I tend to disagree. Pass a value doesn't mean "copy an object". For primitives it copies them, but for class types, it only copies the references.

But now I have a problem with the Set/HashSet. If I have two different SetElem and I add them to the Set, it all works fine. But If I change the instance value of the one SetElem to the same as the other (#1), shouldn't we have duplicates in the set after the equals method? Doesn't look like, the size is still 2 (#2). Even if I remove it (#3) and add it again (#4), all works fine and we have two elements in the set (#5).
Where is my mistake?

Thanks
Bob

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:
I tend to disagree. Pass a value doesn't mean "copy an object". For primitives it copies them, but for class types, it only copies the references.


Agree. Apologies

Bob Wheeler wrote:



you removed k2. then you added . then where the duplicate came?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A HashSet is using a HashMap to store its values. Look at what the java.util.Map tells us : "great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a map."

The golden rule is : values used in equals/hashCode in objects used in collections should be immutable.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:A HashSet is using a HashMap to store its values. Look at what the java.util.Map tells us : "great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a map."

The golden rule is : values used in equals/hashCode in objects used in collections should be immutable.


Thanks for the reply. Really interesting.
But there is still a confusing part for me. If we change the instance variable BEFORE we call the remove-method (#1), the set doesn't recognize the element as
as duplicate. But, if we change the instance variable after the call of the remove-method it does (#2).
Why? Because of this?

The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map


Yes, I guess so. I should stop talking to myself

Thanks Christophe

cheers
Bob

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But there is still a confusing part for me. If we change the instance variable BEFORE we call the remove-method (#1), the set doesn't recognize the element as
as duplicate. But, if we change the instance variable after the call of the remove-method it does (#2).
Why? Because of this?



Basically, when "we change the instance variable BEFORE we call the remove method", we corrupted the set. We were lucky that removed even worked -- as the object could have been in the wrong bucket.

Henry
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find How to Write an Equality Method in Java an interesting read.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys. Got it now. Great link by the way.

cheers
Bob
 
Venkata Saraswathi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh! great link. What a concept it is.....

Clarified lot of doubts with this link.

Thanks,
-- Venkata
 
reply
    Bookmark Topic Watch Topic
  • New Topic