java - Why object of number can be added in TreeSet -
the object of class want add treeset, class must implement comparable interface number class not implement comparable how treeset allow number added.
number n1= 11; number n2= 12; number n3= 13; set<number> set = new treeset<>(); set.add(n1); set.add(n2); set.add(n3);
you right
class must implement comparable interface.
and here in example above statement valid. number has child class integer , when this:
number n1= 11;
on runtime create integer object , integer implement comparable.
you can see in java doc. here
or can proof program this:
number n1 = 10; system.out.println(n1 instanceof number); //true system.out.println(n1 instanceof integer); //true system.out.println(n1 instanceof comparable); //true
hope help!
Comments
Post a Comment