Page 1 of 1
About Question enthuware.ocpjp.v7.2.1301 :
Posted: Sun Sep 15, 2013 1:56 pm
by sinapse
It definitely compile with a warning !
The method set interest is not used ...
Thanks
Re: About Question enthuware.ocpjp.v7.2.1301 :
Posted: Sun Sep 15, 2013 3:13 pm
by admin
I think you are talking about IDE generated warning. The javac compiler has nothing to do with it, which is what we care about for the purpose of this exam.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1301 :
Posted: Thu Dec 18, 2014 11:26 am
by SepticInsect
However, this behavior was changed in the TreeSet implementation recently and it throws a ClassCastException when you add the first element itself.
Was this changed in Java 7?
Re: About Question enthuware.ocpjp.v7.2.1301 :
Posted: Thu Dec 18, 2014 8:27 pm
by admin
Not sure when exactly it was changed but for Java 7 the behavior is as described.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1301 :
Posted: Fri Dec 19, 2014 1:19 am
by SepticInsect
Ok. Thanks!
Re: About Question enthuware.ocpjp.v7.2.1301 :
Posted: Thu Feb 25, 2016 5:14 am
by rianne
Ideally, when you add the first element, since there is nothing to compare this element to, there should be no exception.
If the first element added is-not-a Comparable, there should definitely be a ClassCastException? Since TreeSet only takes objects that are-a Comparable?
Re: About Question enthuware.ocpjp.v7.2.1301 :
Posted: Thu Feb 25, 2016 10:43 am
by admin
rianne wrote:Ideally, when you add the first element, since there is nothing to compare this element to, there should be no exception.
If the first element added is-not-a Comparable, there should definitely be a ClassCastException? Since TreeSet only takes objects that are-a Comparable?
A TreeSet is a Set and to satisfy the Set interface, it has to accept any Object as an argument to the add method. The method implementation can impose additional restriction (such as the argument must be a Comparable) only by checking the object (e.g. obj instanceof Comparable) and throwing an unchecked but this is a design decision. Some time prior to Java 7, they didn't do this. A TreeSet accepted any object. It threw an exception only when a comparison was required to be done.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1301 :
Posted: Thu Aug 23, 2018 1:38 pm
by nowkarol
I wonder if lack of compile time restriction of elements being Comparable is because of constructor with Comparator. If there was no such constructor generic type E could be bounded as E extends Comparable and this error would be moved to compile time.
Am I right?