Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why mutable elements of Sets?

 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Thomas. You're answers were helpful. I am glad I asked.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does it means that we can't say that a set is a collection of unique elements.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic