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

前端 未结 5 1125
臣服心动
臣服心动 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:25

    There are two explanations here:

    1. Historical: Collections class was created after List interface. Designers chose to preserve backward compatibility of already existing interface. Otherwise a lot of developers would have to change their code.

    2. Logical: The methods you are talking about do not require internal knowledge on List implementation and can be implemented over ANY collection implementing it.

提交回复
热议问题