How TreeSet checks for Duplicates

后端 未结 3 792
感情败类
感情败类 2021-02-10 18:06

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:29

    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

提交回复
热议问题