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

前端 未结 5 1833
时光取名叫无心
时光取名叫无心 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:07

    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.

提交回复
热议问题