Creating a TreeSet with a non-Comparable class: why a run-time exception, rather than compile-time error?

前端 未结 1 439
一个人的身影
一个人的身影 2020-12-06 10:15

If I create an arbitrary class that does not implement Comparable, and try to use it as a treeset, it throws an exception at run time when an object is inserted:

<         


        
相关标签:
1条回答
  • 2020-12-06 11:12

    TreeSet is implemented that way because you can alternatively provide a Comparator, in which case the elements don't need to be Comparable. The only way to support both behaviors without splitting the implementation into multiple classes was to include runtime checks - this was simply a design decision by the author(s) of that class.

    Exposing factory methods for TreeSet instead of public constructors would've been a way to maintain compile time checks using stricter generic type constraints, but that would've been a break from the core collections API's convention of exposing public no-arg and copy constructors for its implementation classes. As you noted in your comment, Guava goes the factory route with its collections and IMHO is better off for it.

    0 讨论(0)
提交回复
热议问题