Eric Sosman wrote:
> This is forbidden (or at least discouraged) by the following
> passage from the Javadocs for the Set interface:
>
> "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. [...]"
>
> Instead of the above, use
>
> Thing t = (Thing)(s.last());
> s.remove(t);
> t.setString("Fourth");
> s.add(t);
Thanks Eric & Thomas. I missed that note in the Javadoc
because I was too busy looking for a method that did
what I wanted to do to notice things which explain why
I shouldn't.
I wasn't expecting something that ran in O(log n) time,
but I was initially thinking that a re-sort of some time
would be available -- or at least that constructing a
new one from the old one would just re-insert all the
elements and accomplish what I wanted. But it makes
sense now that I've thought about your remove/add
technique, since for any tree of non-trivial size,
removing and adding a single element is bound to be
more efficient than either method I was hoping for.
Thanks again.