I always learn when we declare a collection we should do, Interface ob = new Class()
, if i want to use for example a LinkedList i'll do List ob = new LinkedList()
, but then i can't have access to all methods from LinkedList.. Isn't LinkedList ob = new LinkedList()
100% correct?
The LinkedList class is a collection which can contain many objects of the same type, just like the ArrayList . The LinkedList class has all of the same methods as the ArrayList class because they both implement the List interface.
The LinkedList class of the Java collections framework provides the functionality of the linked list data structure (doubly linkedlist). Java Doubly LinkedList. Each element in a linked list is known as a node. It consists of 3 fields: Prev - stores an address of the previous element in the list.
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at the contiguous location, the elements are linked using pointers as shown below. In Java, LinkedList can be represented as a class and a Node as a separate class.
You should always try to keep the declaration at the highest level possible, meaning that you should stop at the highest level that provides all the functionality that you need: if List
methods are not enough, you're perfectly fine with your LinkedList
declaration.
Isn't LinkedList ob = new LinkedList() 100% correct?
Well I'd suggest using the generic form, but sure - if you want to use functionality which is specific to LinkedList
, you need to declare the variable accordingly.
You might want to check whether the Deque<E>
or Queue<E>
interfaces have what you want though. If they do, use those in-keeping with the idea of describing what you need rather than what implementation you'll use.
Yes,
LinkedList<...> items = new LinkedList<...>();
is perfectly correct if you know that items
will depend on methods of LinkedList<T>
that are not captured in the List<T>
interface.
If you actually have a need to use methods that are not on the List interface, there is certainly nothing wrong with using LinkedList's API. The general rule of programming to the List interface recognizes that 1) it's pretty rare to need those methods, and 2) in most people's experience, it's way more likely that I discover I need to sort the list and/or use a lot of random access, and decide to switch to an ArrayList, than it is I need one of the methods only LinkedList has.
It may be also that you could be programming to the Queue interface, if you find List isn't giving you what you need.
The rule "always code to interfaces" must be taken with some flexibility. What you are suggesting is fine, and as you came to the conclusion, the only option.
As a side note, coding to concrete classes like this is faster is most JVMs. Deciding whether the performance is worth breaking the rule is the hard thing to decide.
LinkedList is a generic. You should be doing:
LinkedList<String> linkedList = new LinkedList<String>();
(or whatever else you need to store in there instead of String)
Not exactly 100% correct.
A preferred way to declare any collection is to include the data type it's holding. So, for your example, it'd be LinkedList<Integer> ob = new LinkedList<Integer>();
.
Nope.. This would be wrong, at the later stages if he wants to change his implementation from linked list to any other implementation of list type he will go wrong... So better to use the interface level declaration.
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