Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedList different implementations

I have seen 2 valid declarations of the LinkedList class in Collections Framework in Java.

  1. LinkedList a = new LinkedList();
  2. Queue a = new LinkedList();

What exactly is the difference between these 2 implementations?

like image 801
parth Avatar asked Jan 19 '26 05:01

parth


2 Answers

Queue is an interface which LinkedList class implements.

In Your first case you a is a LinkedList reference to a LinkedList object. In the latter, a is a Queue interface reference pointing to a LinkedList object so, in this last case you will be only able to execute those service of LinkedList which are included in Queue interface.

like image 197
Pablo Francisco Pérez Hidalgo Avatar answered Jan 20 '26 18:01

Pablo Francisco Pérez Hidalgo


There is just one implementation in your code: LinkedList.

Queue is one of the multiple interfaces implemented by LinkedList class.

like image 40
Ivan Avatar answered Jan 20 '26 19:01

Ivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!