Why LinkedList and arraylist extends AbstractList in java?

此生再无相见时 提交于 2019-12-04 02:26:42
sanbhat

subList(int,int) method is not overriden by both ArrayList and LinkedList, and for this AbstractList provides a common implementation

From Java source

public List<E> subList(int fromIndex, int toIndex) {
        return (this instanceof RandomAccess ?
                new RandomAccessSubList<E>(this, fromIndex, toIndex) :
                new SubList<E>(this, fromIndex, toIndex));
    }

In addition there are other methods which are not overriden like toString() and iterator()

You can get Answer from Here,,, AbstractList

This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array). For sequential access data (such as a linked list), AbstractSequentialList should be used in preference to this Class. To implement an unmodifiable List, the programmer needs only to extend this class and provide implementations for the get(int index) and size() methods.

To implement a modifiable List, the programmer must additionally override the set(int index, Object element) method (which otherwise throws an UnsupportedOperationException. If the List is variable-size the programmer must additionally override the add(int index, Object element) and remove(int index) methods.

The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification.

Unlike the other abstract Collection implementations, the programmer does not have to provide an Iterator implementation; the iterator and listIterator are implemented by this class, on top the "random access" methods: get(int index), set(int index, Object element), set(int index, Object element), add(int index, Object element) and remove(int index).

The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the Collection being implemented admits a more efficient implementation.

Not all methods from AbstractList are overridden. Remember that AbstractList subclasses AbstractCollection, which defines methods like containsAll or toString which are not overridden by either ArrayList nor LinkedList.

Usage is noted at the top of the AbstractList source file

"This class provides a skeletal implementation of the {@link List} interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array). For sequential access data (such as a linked list), {@link AbstractSequentialList} should be used in preference to this class."

So essentially it provides some methods to build around and a framework that is more robust than the List interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!