Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in LinkedList, queue vs list

What is the difference when creating these two objects

Queue<String> test = new LinkedList<String>(); 

and

List<String> test2 = new LinkedList<String>(); 

What are the actual differences between test and test2? Are both of them LinkedList ? Are there performance differences or reasons to use one over the other?

like image 996
Kailua Bum Avatar asked Mar 25 '13 05:03

Kailua Bum


People also ask

Is linked list a list or queue?

In Java (and probably other languages too), a LinkedList implements the Queue interface. So in essence, a LinkedList is a Queue; it has all features that a Queue does and more. Keep in mind, a Queue is not a LinkedList, as a LinkedList is built and expanded upon a Queue.

What is the difference between stack queue and linked list?

The main difference between Stack and Linked List is that a Stack works according to the FIFO mechanism while a Linked List works by storing the data and the addresses of other nodes to refer to each other. A data structure is a way of storing data elements in computer memory.

Can LinkedList be used as a queue?

Queue supports operations like enqueue and dequeue. It can be implemented using array and linked list.

Which is better queue using array or linked list?

Because allocations are infrequent and accesses are fast, dynamic arrays are usually faster than linked lists.


Video Answer


2 Answers

The two statements you've written each construct a LinkedList<String> object to hold a list of strings, then assign it to a variable. The difference is in the type of the variable.

By assigning the LinkedList<String> to a variable of type Queue<String>, you can only access the methods in the LinkedList that are available in the Queue<String> interface, which includes support for enqueuing and dequeuing elements. This would be useful if you needed to write a program that used a queue for various operations and wanted to implement that queue by using a linked list.

By assigning the LinkedList<String> to a variable of type List<String>, you can only access the methods in the LinkedList that are available in the List<String> interface, which are normal operations for maintaining a sequence of elements. This would be useful, for example, if you needed to process a list of elements that could grow and shrink anywhere.

In short, the two lines create the same object but intend to use them in different ways. One says that it needs a queue backed by a linked list, while the other says that it needs a general sequence of elements backed by a linked list.

Hope this helps!

like image 167
templatetypedef Avatar answered Oct 11 '22 15:10

templatetypedef


In the both the cases, you are instantiating LinkedList.

The difference is the types of the variables you use to refer to those instances.

test is of type Queue and test2 is of type List. Depending on the type of variable, you only get to invoke the methods which are specified on that particular type. I think this what matters for your situation.

Performance-wise, it's going to be the same, because the actual implementation that you are using in both the cases is same (LinkedList).

like image 25
Bhesh Gurung Avatar answered Oct 11 '22 13:10

Bhesh Gurung