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

前端 未结 5 1153
臣服心动
臣服心动 2021-02-19 12:07

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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 12:21

    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.

提交回复
热议问题