Bitten by the Java TreeMap removeAll bug

I had to learn the hard way today about the nasty issue surrounding Java’s TreeSet / TreeMap implementations. The problem is that the removeAll method doesn’t always do what it says it does: remove all elements present in the collection called on that are present in the collection passed as the argument.

Supposedly the problem occurs when the equals and compareTo method in the type contained in the Collections aren’t consistent with each other, and the sizes of the collections compared aren’t equal. Look at the following link for an example
JDK Bug 6394757 : (coll) AbstractSet.removeAll is surprisingly dependent on relative collection sizes.

Anyways, the rule of thumb for me from now is if need be to implement a SortedSet Set that will have either removeAll or retainAll called on, use an HashSet instead, and later use Collections.sort

Set<MyType> mySet = new HashSet<MyType>();
// add some elements, do some operations
mySet.removeAll(anotherCollection);
List<MyType> myList = new ArrayList<MyType>(mySet);
Collections.sort(myList);

Leave a Reply

Your email address will not be published. Required fields are marked *

two × 5 =