Search code examples
javalistcollectionsconceptual

Why is sort method part of Collections instead of AbstractList?


When sort method works upon only Lists, I feel keeping it in Collections class is somewhat buggy. Shouldn't there be a separate class dedicated to Lists? Maybe AbstractList or something like ListUtil.. ?


Solution

  • Collections is a helper class for all collections regardless of their type. In addition to method for the List there are methods specific to Maps, SortedMaps, SortedSets, and, of course, Collections.

    Although making a separate helper for lists alone would be a justifiable choice, using a single helper class as a single place for all collection helpers makes perfect sense as well: it makes it easier for developers to remember where the methods are, because only one class is there.

    Note that starting with Java-8 you can call sort as a default method implementation on List<T>, making the implementation even easier to locate.