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 Programmer Certification (SCJP/OCPJP) and the fly likes Why mutable elements of Sets? 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why mutable elements of Sets?" Watch "Why mutable elements of Sets?" New topic
Author

Why mutable elements of Sets?

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Why are the elements of a Set allowed to be mutable? You could change the value of an element and violate the no duplicates requirement.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And how would you prevent the elements of a set from being mutable? Only allow Strings to be added? Wouldn't that seriously limit the usefulness of the Set?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
For those who aren't sure what we are talking about, take a look at this code:

We now have a Set with duplicate entries. This is very bad because sets will exhibit unpredicatable behavior in this condition.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
My first response was that we only want the fields involved in the computation of the equals method to be immutable. But even that seems to restrictive.
I guess we only want the result of the equals method to be consistent over time. That is, the fields of an object should be allowed to change in any way. But for any two objects o1, o2, we want either o1 == o2 for all time or o1 != o2 for all time. Is that right?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Marlene Miller:
I guess we only want the result of the equals method to be consistent over time. That is, the fields of an object should be allowed to change in any way. But for any two objects o1, o2, we want either o1 == o2 for all time or o1 != o2 for all time. Is that right?

We want the equals not to change while the objects are in the Set. We would not want to make that a general restriction for all objects at all times, however.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you very much Thomas. You're answers were helpful. I am glad I asked.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
So does it means that we can't say that a set is a collection of unique elements.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Anupam, I think I understand why you are asking your question. But just to make sure, could you explain your question.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
What if I get a question that says the following or kinda like that.
Q. Which of the following are gauranteed to have no duplicate entries (according to the equals method).
1. Set
2. Map
3. List
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here are the *contracts* for the Set, List and Map interfaces.
(1) Sets cannot contain duplicate elements.
(2) Unlike Sets, Lists typically allow duplicate elements.
(3) Maps cannot have duplicate keys, but may have duplicate values.
Some people might say they are *not* guarantees, because the programmer can violate the contract. Other people might say they *are* guarantees, because a violation of the contract is a programming error.
Is that the question?
[ May 23, 2003: Message edited by: Marlene Miller ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The contracts of Set and Map are specified by their APIs. The API for Set says:
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.

And for Map:
Note: 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 APIs guarantee there will be no duplicates provided the elements or keys are not changed after insertion. No guarantees are made if this condition is not met.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why mutable elements of Sets?
 
Similar Threads
immutable objects
Mutable
why String class is immutable?
String Vs String Buffer
Name the mutable and immutable Classes