Why no tail() or head() method in List to get last or first element?

前端 未结 8 910
死守一世寂寞
死守一世寂寞 2020-12-29 04:45

I recently had a discussion with a collegue why the List interface in Java doesn\'t have a head() and tail() method.

In order to implement

8条回答
  •  臣服心动
    2020-12-29 05:10

    Java Collections Framework is written by Joshua Bloch. One of his API design principles is: High power-to-weight ratio.

    tail() and head() can be implemented by get() and size(), so it's not necessary to add tail() and head() to a very general interface java.util.List. Once users use the methods, you don't have chance to remove them and you have to maintain these unnecessary methods forever. That's bad.

提交回复
热议问题