Bonjour à tous, si vous cherchez dans l'api vous trouvez ceci piur la méthode contains de java.util.TreeSet :
En suivant le lien vers Set on trouve ceci :contains
public boolean contains(Object o)
Returns true if this set contains the specified element.
Specified by:
contains in interface Set
Overrides:
contains in class AbstractCollection
Parameters:
o - the object to be checked for containment in this set.
Returns:
true if this set contains the specified element.
Throws:
ClassCastException - if the specified object cannot be compared with the elements currently in the set.
Et pourtant en fouillant les sources on tombe sur ceci :contains
public boolean contains(Object o)
Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
Specified by:
contains in interface Collection
Parameters:
o - element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.
Throws:
ClassCastException - if the type of the specified element is incompatible with this set (optional).
NullPointerException - if the specified element is null and this set does not support null elements (optional).
TreeSet#contains(Object o) délègue à la TreeMap#contaisnKey(Object k) qui utilise le résultat de TreeMap#getEntry(Object k) qui utilise TreeMap#compare(Object k1, Object k2) qui fais appel à la méthode Comparable#compareTo(Object o)
donc l'API nous ment. TreeSet utilise compareTo et pas equals.
des réactions ? ou me suis-je gourré ?
Partager