commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grzegorz Ro┼╝niecki (JIRA) <>
Subject [jira] [Commented] (COLLECTIONS-595) ListOrderedSet remove()
Date Fri, 28 Oct 2016 16:57:58 GMT


Grzegorz Ro┼╝niecki commented on COLLECTIONS-595:

If I understand correctly, you modify elements of your type {{MyClass}} after putting them
into set? If so, you're violating set contract[1], which expects immutable elements (or at
least in terms of equals and hashCode):

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.


> ListOrderedSet remove()
> -----------------------
>                 Key: COLLECTIONS-595
>                 URL:
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Set
>    Affects Versions: 4.1
>         Environment: Windows 10, Eclipse Neon Release (4.6.0)
>            Reporter: Maleven
> Hello Apache,
> I have a class which overrides equals() and hasCode().
> I create a ListOrderedSet<MyClass> mySet= new ListOrderedSet<MyClass>(),
and add objects to it:
> mySet.add(obj1);
> etc.
> Each object is initialized in the constructor with two properties first. The other properties
are set afterwards, when
> they are already contained in the ListOrderedSet. After I change/add some properties,
then if I do mySet.contains(obj1), false is returned.
> I read about that when a hashmap is created with the objects inside and then changing
the objects inside it does not change
> the hasmap's internal hashcode for the objects or something like that.
> These properties are found in the equals and hashcode. (When not there is no issue.).
> The following happens. If I still want to remove an object which was changed afterwards,
in the method: public E remove(final int index),
> inside public E remove(final int index) false is returned. But the object is still removed
and in the debugger the following can be seen:
> DEBUG Info:
> == 5 (internal HashMap)
> this.collections.setOrder.size == 4. (internal ArrayList)
> Afterwards if I print the list with the overrided toString() method IndexOutOfBoundsException
is thrown when getting the size of mySet
> via mySet.size(), but not when I use an iterator to count the objects in the set.
>     public int getSize() {
>     	OrderedIterator it = mySet.iterator();
>     	int i = 0;
>     	while(it.hasNext()){
> 		i++;
>     	}
> //if i return i then the items are 4, when returning memorySet.size(), the items are
> //    	return i;
>         return mySet.size();
>     }
>     @Override
>     public String toString() {
>         String toReturn = "\n";
>         for (int i = 0; i < getSize(); i++) {
>             toReturn = toReturn + mySet.get(i) + "\n";
>         }
>         return toReturn;
>     }
> So my question is: is this a bug or is it because of my changing/updating the objects
in the list, and why if it returns false when removing it got deleted anyway?
> Should it not got deleted to start with? 
> Thank you for your effort.
> P.S. I am a self taught programmer. It's my first time sending a bug issue, maybe it's
a bit long or I did not send with it other information I should have.

This message was sent by Atlassian JIRA

View raw message