Java TreeSet sorting -
I found this question from a book, but it was difficult to understand the way behind the answer.
Comparative of public class drink tools {name of public string; Public Ink Comparison (Object O) {Return 0; }}
and:
one = new drink (); Two = drink new (); One.name = "coffee"; Two.name = "tea"; Treceet set = new tree-set (); Set.add (a); Set.add (two);
If a programmer repeats on a tree asset and prints the names of each pie object, what is the result of it?
Can someone please help me in answering the path behind it
Only "coffee" ( a
) is added to the set, because TreeSet equals according to compareto ()
, and since the element is already set, So when you try to add two
, the notice will take place.
Note that the order created by a set (even if a clear comparator has been provided) is equal Should be compatible with this if the set interface is implemented correctly (see comparative or comparative for an exact definition equivalent to the equivalent.) This is because the set interface is defined in terms of equal operation, but A TreeSet example compares this ( Received than) Compares the elements by using the method, so there are two elements which are the approaches that are deemed equal by this method, set equal. The behavior of a set is well defined, even if its sequence is incompatible with equal; It just fails to follow the general agreement of the set interface.
... If this set already has an element, then the call set skips unchanged and returns false.
Also note that this is an inappropriate behavior that does not follow the set
interface agreement, because you did not override equal ()
.
Comments
Post a Comment