Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a LinkedList in Java

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?

like image 777
Márcio Duarte Avatar asked Mar 23 '12 17:03

Márcio Duarte


People also ask

What is the linked list in Java?

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.

What is LinkedList in Java with examples?

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.

How does LinkedList is implemented in Java?

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.


8 Answers

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.

like image 108
michele b Avatar answered Sep 27 '22 14:09

michele b


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.

like image 35
Jon Skeet Avatar answered Sep 29 '22 14:09

Jon Skeet


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.

like image 31
ruakh Avatar answered Sep 29 '22 14:09

ruakh


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.

like image 36
Affe Avatar answered Sep 27 '22 14:09

Affe


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.

like image 25
Jeffrey Blattman Avatar answered Sep 27 '22 14:09

Jeffrey Blattman


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)

like image 21
Eugene Feingold Avatar answered Sep 25 '22 14:09

Eugene Feingold


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>();.

like image 42
Makoto Avatar answered Sep 27 '22 14:09

Makoto


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.

like image 28
Punith Raj Avatar answered Sep 29 '22 14:09

Punith Raj