Return type from a Comparator

前端 未结 6 1415
暖寄归人
暖寄归人 2020-12-09 15:16

What does the return value inside the Comparator actually mean?

For example :

class TreeSetDemo
{
    public static void main(String         


        
6条回答
  •  情深已故
    2020-12-09 15:46

    the way comparator is leveraged here is wrong but for clarifying your doubt why only [20] id being written when return value is 0...

    You are using a TreeSet which doesn't hold identical values(Property of Set). So when the values are compared and return value is zero, Java treats them as equal and retain only the first value. Hence, you are seeing only one value in return.

提交回复
热议问题