So I found that Collection
as an Interface
has many sub-interfaces, and sub-Classes. One thing that is commonly seen is to use the interface instead of using the actual class, which allows flexibility:
List<Interger> L1 = new ArrayList<>();
But this also limits the use of some special methods in the actual Class
, (in the case of ArrayList
, not so many special methods). But this can be solved by using casting.
List<Integer> L2 = ((ArrayList<Integer>) L1).clone();
However, it is interesting that LinkedList
is implementing Collection
, List
, Queue
, Deque
.
So what is the best scenario of using LinkedList
in common?
List<> L1 = new LinkedList<>();
Queue<> Q1 = new LinkedList<>();
Deque<> D1 = new LinkedList<>();
- If I use
List
, well then it seems unnecessary because I can makeList
fromArrayList
. - Should I always choose the
Interface
that is just 1 level above theClass
, in this case,Deque
overQueue
?
I guess it is just confusing because Queue
and Deque
does not implement List
, but LinkedList
does. And I don't see why should it be?
Thanks,