In the Collection Framework we have the interface List
and the class AbstractList
:
AbstractList implements List
And ArrayList
extends AbstractList
and
implements List
My question: why does ArrayList
have the implements List
clause?
If ArrayList extends AbstractList
and AbstractList implements List
, can't we say, that ArrayList implement List
?
ArrayList allows duplicate values in its collection. On other hand duplicate elements are not allowed in Hashset. HashSet is completely based on object also it doesn't provide get() method.
List allows duplicates while Set doesn't allow duplicate elements . All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value. List permits any number of null values in its collection while Set permits only one null value in its collection.
ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.
Yes. It could've been omitted. But thus it is immediately visible that it is a List
. Otherwise an extra click through the code / documentation would be required. I think that's the reason - clarity.
And to add what Joeri Hendrickx commented - it is for the purpose of showing that ArrayList
implements List
. AbstractList
in the whole picture is just for convenience and to reduce code duplication between List
implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With