How TreeSet checks for Duplicates

后端 未结 3 495
耶瑟儿~
耶瑟儿~ 2021-02-10 18:02

I am checking how TreeSet checks for duplicate elements and have the following code

  import java.util.*;

  public class TreeDemo{

    public static void main(         


        
3条回答
  •  忘掉有多难
    2021-02-10 18:17

    a comparator return < 0, 0 or > 0... So equals is implemented by compareTo returning 0. Thus

    if (node1.compareTo(node2) == 0) 
    

    then the node is already in the set

提交回复
热议问题