Why does the Collections class contain standalone (static) methods, instead of them being added to the List interface?

前端 未结 5 1836
时光取名叫无心
时光取名叫无心 2021-02-19 11:41

For all the methods in Collections that take a List as their first argument, why aren\'t those methods simply part of the List interface?

My intuition is: given a List

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 12:09

    The point is that given suitable primitive operations (remove, set etc) a bunch of more high level operations (sort, shuffle, binary search) can be implemented once rather than being implemented by every single list implementation.

    Effectively, java.util.Collections is like .NET's Enumerable class - full of general purpose methods which can work on any collection, so that they can share a single implementation and avoid duplication.

提交回复
热议问题