This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes HashSet allowing duplicates; please explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashSet allowing duplicates; please explain" Watch "HashSet allowing duplicates; please explain" New topic
Author

HashSet allowing duplicates; please explain

Joe May
Greenhorn

Joined: Sep 28, 2009
Posts: 22


if you see the above snippet.. i am modifying the object within a set and making it equal to another element in the set. After this modification, set s1 has duplicate elements. When i now try to add both the elements of set s1 into set s2, addition of second element in set s2 fails. It returns false. But no exception happens while modifying set s1. Can some one please explain this?
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
From the javadoc for java.util.Set:

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

rc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashSet allowing duplicates; please explain
 
Similar Threads
How to catch Runtime error
How to print the Output
TreeSet with compartor constructor
sorting arraylist of arraylist
please explain the output?