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

前端 未结 8 901
死守一世寂寞
死守一世寂寞 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:33

    head() is provided via list.iterator().next(), list.get(0), etc.

    It is only reasonable to provide tail() if the list is doubly linked with a tail pointer, or based on an array, etc., Neither of these aspects is specified for the List interface itself. Otherwise it could have O(N) performance.

提交回复
热议问题