Skip to content
Advertisement

I made my own Collection based on TreeSet but I have troubles with existing test: it doesn’t see elements

Here is my collection. Here I try to make my own implemetation with special Comparator that sorts Integer elements by its absolute values.

JavaScript

(and other methods)

Main class. Everything seems to work correct except toString() method. When I overwrite this method without lambdas it works. But! This method is in tests and I mustn’t change it. I just copied it to Main class trying to understand the problem. And problem I want to solve is somewhere in SortedByAbsoluteValueIntegerSet class.

JavaScript

This is another realization that works good. So what’s the difference?

JavaScript

Advertisement

Answer

You shouldn’t be extending TreeSet and having a TreeSet field. One or the other, but both makes no sense.

This is probably actually the cause of your issue: you have two different TreeSets associated with each SortedByAbsoluteValueIntegerSet, and the one you’re adding to and the one toString() is getting are different.

Extend AbstractSet instead.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement