1

The Sun has provided the TreeSet and the TreeMap and but no TreeList. They provided a utility Collections.sort() to sort the List. When they provided the Sorted Map and Set, what is the reason they didn't provide the Sorted List.?

Is there any specific reason behind it.?

I am preparing for SCJP, so while going through the Generics and Collections, I got this doubt. can anyone clarify.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
Lakshmi
  • 81
  • 2
  • 8
  • Please read this http://stackoverflow.com/faq#dontask – vascowhite Jul 23 '12 at 06:13
  • They have provided custom comparators as well as binary search, therefore, you have all you need to produce a SortedList class. – ioreskovic Jul 23 '12 at 06:15
  • And `TreeList` does not really warrant an implementation because it will not be faster than a general `ArrayList` except at insertions in the middle and removes (which is very rarely performed anyway and if you're doing a lot of those, you should be using a Set :) – Strelok Jul 23 '12 at 06:19

1 Answers1

4

First line in the List API says it is an ordered collection (also known as a sequence). If you sort the list you can't maintain the order, so there is no TreeList in Java. Probably that is the case you need to externally sort it as you have mentioned.
As API says Java List got inspired from Sequence and see the sequence properties http://en.wikipedia.org/wiki/Sequence_(mathematics)

It doesn't mean that you can't sort the list, but Java strict to his definition and doesn't provide sorted versions of lists by default.

Syam
  • 1,202
  • 11
  • 23