The implementation of java.util.ArrayList
implements List
as well as extends AbstractList
. But in java docs you can see that AbstractList already implements List. Then wouldn't it be redundant to implement List as well as extend AbstractList?
My second question
Please have a look at the following code :
String str = "1,2,3,4,5,6,7,8,9,10"; String[] stra = str.split(","); List<String> a = Arrays.asList(stra);
The Arrays.asList()
method of the Arrays class contains its own implementation of ArrayList. But this one only extends AbstractList but does not implement List. But the above code compiles.
BUT when the code is modified to the following
String str = "1,2,3,4,5,6,7,8,9,10"; String[] stra = str.split(","); java.util.ArrayList<String> a = Arrays.asList(stra);
I get an error : cannot convert form List<String> to ArrayList<String>
What is the reason behind this?
EDITArrays.asList()
does return its own implementation of ArrayList. Check this out.
The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.
ArrayList inherits AbstractList class and implements the List interface. ArrayList is initialized by the size.
Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.
In short, ArrayList is more flexible than a plain native array because it's dynamic. It can grow itself when needed, which is not possible with the native array. ArrayList also allows you to remove elements which are not possible with native arrays.
Then wouldn't it be redundant to implement List as well as extend AbstractList?
Yes, it is 100% redundant. However, Java implementors added interfaces very consistently in all public implementation of the collections library:
LinkedList<E>
and ArrayList<E>
extend AbstractList<E>
which implements List<E>
, and then implement List<E>
themselves.HashSet<E>
and TreeSet<E>
extend AbstractSet<E>
which implements Set<E>
, and then implement Set<E>
themselves.HashMap<K,V>
and TreeMap<E>
extend AbstractMap<K,V>
which implements Map<K,V>
, and then implement Map<K,V>
themselves.My understanding is that they did so for documentation purposes: the authors wanted to show that ArrayList<E>
is primarily a List<E>
; the fact that ArrayList<E>
extends AbstractList<E>
is a less significant detail of its implementation. Same goes for the other public collection types.
Note that Arrays.ArrayList<E>
class is not publicly visible, so its authors did not care to include List<T>
explicitly.
As far as the failed conversion goes, this should come as no surprise, because the inner class Arrays.ArrayList<E>
and the public class ArrayList<E>
are unrelated to each other.
For your first question take a look at Why does ArrayList have "implements List"?
To answer your second question
java.util.ArrayList<String> a = Arrays.asList(stra);
as you mentioned Arrays.asList
returns its own implementation of AbstractList and unfortunately creators of this code also named this class ArrayList. Now because we cant cast horizontally but only vertically returned array list can't be cast to java.utli.ArrayList
but only to java.util.AbstractList
or its super types like java.util.List
that is why your first code example works.
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