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?
seetharaman venkatasamy wrote:Java Class method argument always pass by value[copy of a object ] .
SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
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.
Bob Wheeler wrote:
[My Blog]
All roads lead to JavaRanch
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.
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
SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
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?
SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq