linkedlist vs arraylist- an in depth look at java.util.linkedlist
TRANSCRIPT
![Page 1: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/1.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.LinkedList
Marcus Biel, Software Craftsmanhttp://www.marcus-biel.com
![Page 2: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/2.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List23 3 17 9 42
In the previous episode I introduced you to the Linked List data structure.
![Page 3: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/3.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List23 3 17 9 42
As the name implies, the Java class LinkedList is called LinkedList
because internally it is based on a Doubly Linked List.
![Page 4: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/4.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Concept vs. ImplementationSo what is the difference between the LinkedList data
structure and
the class java.util.LinkedList?
![Page 5: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/5.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Concept vs. Implementation
As an analogy, think of the abstract concept of a car and a concrete car.
![Page 6: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/6.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Concept vs. Implementation
The Linked List data structure is an abstract concept, independent of any specific programming language.
![Page 7: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/7.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Concept vs. Implementation
The LinkedList Java class is a concrete implementation of this abstract concept.
![Page 8: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/8.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.LinkedList
ImplementsExtends
LinkedList
So in this tutorial, I will focus on one specific Linked List implementation, the java.util.LinkedList class.
![Page 9: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/9.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.LinkedList
ImplementsExtends
<<interface>>List
LinkedList
Among other interfaces, LinkedList implements the java.util.List interface.
![Page 10: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/10.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.LinkedList
ImplementsExtends
<<interface>>List
LinkedList
You can have duplicate elements in a List and you can go from element to element in the same order
as the elements were inserted.
![Page 11: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/11.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
ImplementsExtends
<<interface>>
List
ArrayList LinkedList
In a previous tutorial, I introduced you to the java.util.ArrayList class.
![Page 12: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/12.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
ImplementsExtends
<<interface>>
List
ArrayList LinkedList
As you can see, both classes implement the List interface, which makes them somewhat similar.
So what’s the difference between ArrayList and LinkedList?
![Page 13: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/13.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
0 1 2 3 4
23 3 17 9 42
ArrayList vs. LinkedList
First of all, ArrayList is based on an Array data structure,
![Page 14: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/14.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
0 1 2 3 4
23 3 17 9 42
23 3 17 9 42
ArrayList vs. LinkedList
while LinkedList is based on a Doubly Linked List data structure.
![Page 15: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/15.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
0 1 2 3 4
23 3 17 9 42
23 3 17 9 42
ArrayList vs. LinkedList
Compared to an ArrayList, the Doubly Liked List data structure of the LinkedList class
allows more efficient insertion and removal of elements at any position within the List.
![Page 16: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/16.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList23 3 17 9 42
Therefore, as an implementation of the List interface prefer
LinkedList over ArrayList if your main use is to
add or remove elements at random positions in the List.
![Page 17: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/17.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList0 1 2 3 4
23 3 17 9 42
Otherwise, ArrayList might be a better choice, because storing elements in an array consumes less
memory and generally gives faster access times.
![Page 18: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/18.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
ImplementsExtends
<<interface>>
Collection
<<interface>>
List
<<interface>>
Queue
LinkedList
<<interface>>
Deque
ArrayList
Besides the different data structures of ArrayList and LinkedList
LinkedList also implements the Queue and the Deque interfaces
which gives it some additional functionality over ArrayList.
![Page 19: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/19.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
ImplementsExtends
<<interface>>
Collection
<<interface>>
List
<<interface>>
Queue
LinkedList
<<interface>>
Deque
ArrayList
In conclusion, there is no overall winner between ArrayList and LinkedList.
Your specific requirements will determine which class to use.
![Page 20: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/20.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
LinkedList
ImplementsExtends
<<interface>>
Collection
<<interface>>
List
<<interface>>
Queue
LinkedList
<<interface>>
Deque
Let’s put ArrayList aside for now and have an in-depth look at the LinkedList implementation.
![Page 21: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/21.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
LinkedList
Here is a simplified code excerpt from the java.util.LinkedList class.
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
![Page 22: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/22.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
LinkedList
I don’t expect you to fully grasp every detail of the code, I just want to show you that
LinkedList is a normal Java class which anyone could have written, given enough time and knowledge.
![Page 23: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/23.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
LinkedList
The real source code is available online. After finishing this presentation,
I recommend that you take a look at it for yourself.
![Page 24: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/24.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
LinkedList
So, as you can see, LinkedList implements the List, Queue and Deque interfaces,
as Deque extends the Queue interface.
![Page 25: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/25.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
LinkedList
Next you can see that the LinkedList class has a reference to the first and the last elements of the list.
![Page 26: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/26.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
LinkedList
Finally, you can see that the class has functions like- get, add or remove
to access, insert or delete elements from the list.
![Page 27: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/27.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List23 3 17 9 42
As we just saw in the code, the LinkedList class has a reference to the first
and last elements of the list, shown as red arrows in this slide.
![Page 28: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/28.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List23 3 17 9 42
Every single element in a Doubly Linked List has a reference to
its previous and next elements as well as a reference to an item,
simplified as a number within a yellow box on this slide.
![Page 29: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/29.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
public class Node<E> { private E item; private Node<E> previous; private Node<E> next; public Node(E element, Node<E> previous, Node<E> next) { this.item = element; this.next = next; this.previous = previous; }}
Node
Here you see a code excerpt of a Node. It has private members for the item it holds, and for the previous and next Node in the list.
![Page 30: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/30.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
LinkedList
As a user of the Collections class LinkedList, you never directly access the Nodes.
![Page 31: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/31.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
package java.util;
public class LinkedList<E> implements List<E>,Deque<E>{
private Node<E> first; private Node<E> last;
public E get(int index) {…} public boolean add(E e) {…} public E remove(int index) {…}
[…]}
LinkedList
Instead you use the public methods of the LinkedList class that internally operate on the private Node members.
![Page 32: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/32.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.List<<interface>
>List
LinkedList
In my tutorial about ArrayList , I introduced you to the methods of the List interface,
so I won’t mention about those methods again.
![Page 33: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/33.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Queue<<interface>
>Queue
LinkedList
Instead, let’s go on and look at the methods of the Queue interface implemented by LinkedList.
![Page 34: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/34.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Operations on a Queue
end (tail)
front (head)23 3 17 9 42
From a high level perspective, the Queue interface consists of three simple operations:
![Page 35: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/35.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
add elementretrieve elementretrieve and remove element
Operations on a Queue
add an element to the end of the Queue
![Page 36: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/36.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
add elementretrieve elementretrieve and remove element
Operations on a Queue
retrieve an element from the front of the Queue, without removing it.
![Page 37: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/37.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
add elementretrieve elementretrieve and remove element
Operations on a Queue
but of course the operation returns a reference to the object and does not copy it.
![Page 38: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/38.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
add elementretrieve elementretrieve and remove element
Operations on a Queue
Okay. Finally you can retrieve and remove an element from the front of the Queue.
![Page 39: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/39.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Specific Events on a Queue
In the lifetime of a Queue, there are special situations,
![Page 40: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/40.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Specific Events on a Queue
?
like trying to remove an element…from an empty Queue
![Page 41: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/41.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Specific Events on a Queue
or trying to add an element to a Queue that has a limited capacity
and is currently full.
23 3 17 9 42 39 25 11 16 20 34
![Page 42: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/42.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
return special valuethrow Exception
Specific Events on a Queue
Depending on your specific implementation, this might be an expected situation and
you need a method that returns null or false in this case.
![Page 43: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/43.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
return special valuethrow Exception
Specific Events on a Queue
Alternatively this might be an unexpected situation and you need a method that throws an Exception in this case.
![Page 44: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/44.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.QueueThrows Exception Returns Special
Value
Add add Offer
Retrieve element Peek
Retrieve & Remove Remove Poll
The Queue interface offers each of its operations in two flavours –
one method that will throw an Exception, and one that will return a special value in certain cases.
let’s look at this in more detail.
![Page 45: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/45.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsboolean add(E e)boolean offer(E e)
A Queue allows to add elements to the end of the Queue.
![Page 46: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/46.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsboolean add(E e)boolean offer(E e)
“add” will throw an Exception when the Queue is full, while “offer” will return false in this case.
![Page 47: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/47.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsboolean add(E e)boolean offer(E e)
LinkedList, like most Queue implementations, has an unlimited capacity, so it will never be full.
![Page 48: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/48.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsboolean add(E e)boolean offer(E e)
ArrayBlockingQueue on the other hand is a Queue implementation that has a limited capacity.
![Page 49: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/49.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsE element()E peek()
Next, “element” and “peek” allow you to retrieve an element from the front of the
Queue, without removing it.
![Page 50: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/50.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsE element()E peek()
If the Queue is empty, the element function will throw an Exception,
while peek() will return false.
![Page 51: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/51.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elementsE remove()E poll()
Finally you can retrieve and remove an element from the front of the Queue.
If the Queue is empty, remove will throw an Exception, while poll will return false.
![Page 52: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/52.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Deque<<interface>
>Deque
LinkedList
Okay, now we will look at some methods of the Deque interface,
as implemented by LinkedList.
![Page 53: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/53.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Deque<<interface>
>Deque
LinkedList
Deque is the short form of “Double Ended Queue”
![Page 54: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/54.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Deque<<interface>
>Deque
LinkedList
so it is a Queue that can be accessed from either end.
![Page 55: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/55.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Dequethrows Exception returns special
value
Add addFirstaddLast offerFirstofferLast
Retrieve getFirstgetLast peekFirstpeekFirst
Retrieve & Remove removeFirstremoveLast pollFirstpollLast
Just like a Queue, a Deque allows adding, retrieving and - retrieving and removing - an element.
![Page 56: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/56.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Dequethrows Exception returns special
value
Add addFirstaddLast offerFirstofferLast
Retrieve getFirstgetLast peekFirstpeekFirst
Retrieve & Remove removeFirstremoveLast pollFirstpollLast
But as it can be accessed from either end, the Queue methods we saw before now exist in two
variations – one for the first and one for the last element in the Deque.
![Page 57: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/57.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Dequethrows Exception returns special
value
Add addFirstaddLast offerFirstofferLast
Retrieve getFirstgetLast peekFirstpeekFirst
Retrieve & Remove removeFirstremoveLast pollFirstpollLast
Again, let’s look at this in more detail.
![Page 58: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/58.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
You can add elements to both ends of the Deque.
![Page 59: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/59.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsvoid addFirst(E e)
Just like the add method of the Queue interface, addFirst
![Page 60: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/60.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsvoid addFirst(E e)void addLast(E e)
and addLast will throw an Exception when the Deque is full.
![Page 61: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/61.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
3 17 9
42
boolean offerFirst(E e)
“offerFirst”…
![Page 62: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/62.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
3 17 9
22
boolean offerFirst(E e)boolean offerLast(E e)…and “offerLast” will return false
instead of throwing an Exception.
![Page 63: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/63.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsboolean offerFirst(E e)boolean offerLast(E e)
Please keep in mind that LinkedList has an unlimited capacity,
so it will never be full.
![Page 64: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/64.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Add elementsboolean offerFirst(E e)boolean offerLast(E e)
LinkedBlockingDeque on the other hand is a Deque implementation-that may have a limited capacity.
Okay, let’s go on.
![Page 65: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/65.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsYou can retrieve elements from both ends of the Deque,
without removing them.
![Page 66: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/66.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsE getFirst()
“getFirst”…
![Page 67: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/67.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsE getFirst()E getLast()
and “getLast” will throw an Exception when the Queue is empty,
![Page 68: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/68.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsE peekFirst()
while “peekFirst”
![Page 69: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/69.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsE peekFirst()E peekLast()
and “peekLast” will return false in this case.
![Page 70: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/70.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsFinally,
you can retrieve and remove elements from both ends of the Deque.
![Page 71: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/71.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elementsE removeFirst()
“removeFirst”
![Page 72: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/72.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elementsE removeFirst()E removeLast()
and “removeLast” will throw an Exception when the Queue is empty,
![Page 73: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/73.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elementsE pollFirst()
while pollFirst
![Page 74: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/74.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elementsE pollFirst()E pollLast()
and pollLast will return false in this case.
![Page 75: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/75.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
StackOkay. Now on to a completely different topic.
The Deque interface also supports the methods of the Stack data structure,
“push” “peek” and “pop”.
![Page 76: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/76.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Stack
Therefore java.util.LinkedList can also be used as Stack.
![Page 77: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/77.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Stack
A Stack is a very simple data structure,that can only be accessed from the top. As an analogy, think of a stack of books.
![Page 78: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/78.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Stackboolean push (E e)
“push” adds an element to the top of the Stack.
![Page 79: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/79.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Stackboolean push (E e)
It is equivalent to the “addFirst” method.
![Page 80: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/80.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
StackE peek()
“peek” retrieves but does not remove an element from the top of the Stack.
![Page 81: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/81.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
StackE peek()
It is equivalent to the “peekFirst” method.
![Page 82: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/82.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
StackE pop()
“pop” retrieves and removes an element from the top of the Stack.
![Page 83: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/83.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
StackE pop()
It is equivalent to the “removeFirst” method.
![Page 84: LinkedList vs Arraylist- an in depth look at java.util.LinkedList](https://reader035.vdocument.in/reader035/viewer/2022070515/587ad7341a28ab542b8b470f/html5/thumbnails/84.jpg)
2015, Marcus Biel, http://www.marcus-biel.com/
Copyright © 2016 Marcus Biel
All rights reserved