You are correctly printing the set.
You just haven't implemented the Signal.toString()
method to represent a Signal
object the way you want it.
About updating objects in a TreeSet
: the object must not be modified in such a way that the output of compareTo
is changed. If it needs to be updated in this way, remove it (using Set.remove(object)
, update it and add it back.
That said, your compareTo()
method always returns 0. It should return arg0.occurence - occurence
if you want higher occurence
values to come before lower ones.
This is because returning a value less than 0 means this
comes before the argument. 0 means equal ordering and > 1 means this
comes after the argument.
Also, if you implement compareTo()
you define the natural ordering of the class. It is strongly recommended to also override equals()
and return true
if and only if compareTo()
returns 0. See http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html (search for 'consistent with equals'). With the above implementation of compareTo()
, this would mean returning true
only when they both have the same occurence
value. If you don't want to implement equals this way (it is probably not correct) then you should write a class that implements Comparator<Signal>
and pass that to the TreeSet
constructor.