SlideShare a Scribd company logo
 2015, Marcus Biel, http://www.marcus-biel.com/
Marcus Biel, Software Craftsman
http://www.marcus-biel.com
 2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List
23 3 17 9 42
In the previous episode I introduced you to the Linked List data structure.
 2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List
23 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Concept vs. Implementation
So what is the difference between the LinkedList data structure
and
the class java.util.LinkedList?
 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.
 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Concept vs. Implementation
The LinkedList Java class is a concrete implementation
of this abstract concept.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.LinkedList
Implements
Extends
LinkedList
So in this tutorial, I will focus on one specific
Linked List implementation,
the java.util.LinkedList class.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.LinkedList
Implements
Extends
<<interface>>
List
LinkedList
Among other interfaces,
LinkedList implements the java.util.List interface.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.LinkedList
Implements
Extends
<<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.
 2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
Implements
Extends
<<interface>>
List
ArrayList LinkedList
In a previous tutorial,
I introduced you to the java.util.ArrayList class.
 2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
Implements
Extends
<<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?
 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,
 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.
 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
23 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
0 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
Implements
Extends
<<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.
 2015, Marcus Biel, http://www.marcus-biel.com/
ArrayList vs. LinkedList
Implements
Extends
<<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.
 2015, Marcus Biel, http://www.marcus-biel.com/
LinkedList
Implements
Extends
<<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.
 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) {…}
[…]
}
 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.
 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.
 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.
 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.
 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List
23 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Doubly Linked List
23 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.
 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.
 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.
 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.
 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.
 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.
 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:
 2015, Marcus Biel, http://www.marcus-biel.com/
add element
retrieve element
retrieve and remove element
Operations on a Queue
add an element to the end of the Queue
 2015, Marcus Biel, http://www.marcus-biel.com/
add element
retrieve element
retrieve and remove element
Operations on a Queue
retrieve an element from the front of the Queue,
without removing it.
 2015, Marcus Biel, http://www.marcus-biel.com/
add element
retrieve element
retrieve and remove element
Operations on a Queue
but of course the operation returns a reference to the object
and does not copy it.
 2015, Marcus Biel, http://www.marcus-biel.com/
add element
retrieve element
retrieve and remove element
Operations on a Queue
Okay. Finally you can retrieve and remove
an element from the front of the Queue.
 2015, Marcus Biel, http://www.marcus-biel.com/
Specific Events on a Queue
In the lifetime of a Queue, there are special situations,
 2015, Marcus Biel, http://www.marcus-biel.com/
Specific Events on a Queue
?
like trying to remove an element…
from an empty Queue
 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
 2015, Marcus Biel, http://www.marcus-biel.com/
return special value
throw 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
return special value
throw 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Queue
Throws 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
boolean add(E e)
boolean offer(E e)
A Queue allows to add elements to the end of the Queue.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
boolean 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
boolean add(E e)
boolean offer(E e)
LinkedList, like most Queue implementations,
has an unlimited capacity, so it will never be full.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
boolean add(E e)
boolean offer(E e)
ArrayBlockingQueue on the other hand is a
Queue implementation that has a limited capacity.
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
E element()
E peek()
Next, “element” and “peek”
allow you to retrieve an element from the front of the Queue,
without removing it.
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
E element()
E peek()
If the Queue is empty,
the element function will throw an Exception,
while peek() will return false.
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elements
E 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.
 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Deque
<<interface>>
Deque
LinkedList
Deque is the short form of “Double Ended Queue”
 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Deque
throws Exception returns special value
Add addFirst addLast offerFirst offerLast
Retrieve getFirst getLast peekFirst peekFirst
Retrieve & Remove
removeFirst removeLas
t
pollFirst pollLast
Just like a Queue, a Deque allows adding, retrieving
and - retrieving and removing - an element.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Deque
throws Exception returns special value
Add addFirst addLast offerFirst offerLast
Retrieve getFirst getLast peekFirst peekFirst
Retrieve & Remove
removeFirst removeLas
t
pollFirst pollLast
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.
 2015, Marcus Biel, http://www.marcus-biel.com/
java.util.Deque
throws Exception returns special value
Add addFirst addLast offerFirst offerLast
Retrieve getFirst getLast peekFirst peekFirst
Retrieve & Remove
removeFirst removeLas
t
pollFirst pollLast
Again, let’s look at this in more detail.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
You can add elements to both ends of the Deque.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
void addFirst(E e)
Just like the add method of the Queue interface, addFirst
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
void addFirst(E e)
void addLast(E e)
and addLast will throw an Exception when the Deque is full.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
3 17 9
42
boolean offerFirst(E e)
“offerFirst”…
 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
boolean offerFirst(E e)
boolean offerLast(E e)
Please keep in mind that LinkedList has an unlimited capacity,
so it will never be full.
 2015, Marcus Biel, http://www.marcus-biel.com/
Add elements
boolean 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
You can retrieve elements from both ends of the Deque,
without removing them.
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
E getFirst()
“getFirst”…
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
E getFirst()
E getLast()
and “getLast” will throw an Exception when the Queue is empty,
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
E peekFirst()
while “peekFirst”
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
E peekFirst()
E peekLast()
and “peekLast” will return false in this case.
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
Finally,
you can retrieve and remove elements from both ends of the Deque.
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve elements
E removeFirst()
“removeFirst”
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elements
E removeFirst()
E removeLast()
and “removeLast” will throw
an Exception when the Queue is empty,
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elements
E pollFirst()
while pollFirst
 2015, Marcus Biel, http://www.marcus-biel.com/
Retrieve & remove elements
E pollFirst()
E pollLast()
and pollLast will return false in this case.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
Okay. Now on to a completely different topic.
The Deque interface also supports
the methods of the Stack data structure,
“push” “peek” and “pop”.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
Therefore java.util.LinkedList can also be used as Stack.
 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.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
boolean push (E e)
“push” adds an element to the top of the Stack.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
boolean push (E e)
It is equivalent to the “addFirst” method.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
E peek()
“peek” retrieves
but does not remove an element from the top of the Stack.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
E peek()
It is equivalent to the “peekFirst” method.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
E pop()
“pop” retrieves and removes an element from the top of the Stack.
 2015, Marcus Biel, http://www.marcus-biel.com/
Stack
E pop()
It is equivalent to the “removeFirst” method.
 2015, Marcus Biel, http://www.marcus-biel.com/
Copyright © 2016
Marcus Biel
All rights reserved

More Related Content

PPTX
Collections framework in java
ODP
Java Collections
PDF
5 collection framework
PDF
Java Serialization
PPT
Java Collections Framework
PPTX
How Hashmap works internally in java
PDF
Java Collections Tutorials
PPT
Java collection
Collections framework in java
Java Collections
5 collection framework
Java Serialization
Java Collections Framework
How Hashmap works internally in java
Java Collections Tutorials
Java collection

What's hot (20)

PDF
Collections Api - Java
PDF
Java Collection framework
PPTX
Exceptions in Java
PPT
L11 array list
PPT
jQuery Ajax
PPTX
Java Stack Data Structure.pptx
PPTX
Java 8 - Features Overview
PDF
Spring Framework - Spring Security
PDF
Collectors in the Wild
PPTX
L21 io streams
PPT
MySQL ppt
PDF
Spring boot introduction
PPTX
Lecture - 2 Environment setup & JDK, JRE, JVM
PPTX
JavaScript Core fundamentals - Learn JavaScript Here
PPTX
C# classes objects
PDF
JPA and Hibernate
PPTX
Array of objects.pptx
PPSX
Collections - Maps
PPTX
Singleton Pattern (Sole Object with Global Access)
PPTX
Packages,static,this keyword in java
Collections Api - Java
Java Collection framework
Exceptions in Java
L11 array list
jQuery Ajax
Java Stack Data Structure.pptx
Java 8 - Features Overview
Spring Framework - Spring Security
Collectors in the Wild
L21 io streams
MySQL ppt
Spring boot introduction
Lecture - 2 Environment setup & JDK, JRE, JVM
JavaScript Core fundamentals - Learn JavaScript Here
C# classes objects
JPA and Hibernate
Array of objects.pptx
Collections - Maps
Singleton Pattern (Sole Object with Global Access)
Packages,static,this keyword in java
Ad

Viewers also liked (8)

PPT
Singly link list
PPTX
PPS
Single linked list
PPT
Link List
PDF
Collections In Java
PDF
Free your lambdas
PDF
Linked to ArrayList: the full story
PPTX
Linked list
Singly link list
Single linked list
Link List
Collections In Java
Free your lambdas
Linked to ArrayList: the full story
Linked list
Ad

Similar to LinkedList vs Arraylist- an in depth look at java.util.LinkedList (20)

PPTX
Chapter 4 Linked List introduction lessons.pptx
PPTX
Linked list
PDF
Linked list
PPTX
Linked List data structure
PDF
Linked list (java platform se 8 )
PPT
Doubly Circular Linked List – Both next and previous pointers form a circular...
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPTX
Linked list
PPT
Data structures
PPT
Java Collection fundamentals and Uses Unit
PPT
PPT
PPT
PDF
LinkedList vs ArrayList in Java | Edureka
PDF
Linked List Objective The purpose of this exercise is to cr.pdf
PPT
topic11LinkedLists.ppt
PPTX
Linked list, Singly link list and its operations
PDF
java unit 4 pdf - about java collections
PPTX
Data Structures(Part 1)
PPT
topic11LinkedLists.ppt
Chapter 4 Linked List introduction lessons.pptx
Linked list
Linked list
Linked List data structure
Linked list (java platform se 8 )
Doubly Circular Linked List – Both next and previous pointers form a circular...
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Linked list
Data structures
Java Collection fundamentals and Uses Unit
LinkedList vs ArrayList in Java | Edureka
Linked List Objective The purpose of this exercise is to cr.pdf
topic11LinkedLists.ppt
Linked list, Singly link list and its operations
java unit 4 pdf - about java collections
Data Structures(Part 1)
topic11LinkedLists.ppt

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Lesson notes of climatology university.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial diseases, their pathogenesis and prophylaxis
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
Computing-Curriculum for Schools in Ghana
202450812 BayCHI UCSC-SV 20250812 v17.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
VCE English Exam - Section C Student Revision Booklet
102 student loan defaulters named and shamed – Is someone you know on the list?
Anesthesia in Laparoscopic Surgery in India
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf

LinkedList vs Arraylist- an in depth look at java.util.LinkedList

  • 1.  2015, Marcus Biel, http://www.marcus-biel.com/ Marcus Biel, Software Craftsman http://www.marcus-biel.com
  • 2.  2015, Marcus Biel, http://www.marcus-biel.com/ Doubly Linked List 23 3 17 9 42 In the previous episode I introduced you to the Linked List data structure.
  • 3.  2015, Marcus Biel, http://www.marcus-biel.com/ Doubly Linked List 23 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.
  • 4.  2015, Marcus Biel, http://www.marcus-biel.com/ Concept vs. Implementation So what is the difference between the LinkedList data structure and the class java.util.LinkedList?
  • 5.  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.
  • 6.  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.
  • 7.  2015, Marcus Biel, http://www.marcus-biel.com/ Concept vs. Implementation The LinkedList Java class is a concrete implementation of this abstract concept.
  • 8.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.LinkedList Implements Extends LinkedList So in this tutorial, I will focus on one specific Linked List implementation, the java.util.LinkedList class.
  • 9.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.LinkedList Implements Extends <<interface>> List LinkedList Among other interfaces, LinkedList implements the java.util.List interface.
  • 10.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.LinkedList Implements Extends <<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.
  • 11.  2015, Marcus Biel, http://www.marcus-biel.com/ ArrayList vs. LinkedList Implements Extends <<interface>> List ArrayList LinkedList In a previous tutorial, I introduced you to the java.util.ArrayList class.
  • 12.  2015, Marcus Biel, http://www.marcus-biel.com/ ArrayList vs. LinkedList Implements Extends <<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?
  • 13.  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,
  • 14.  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.
  • 15.  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.
  • 16.  2015, Marcus Biel, http://www.marcus-biel.com/ ArrayList vs. LinkedList 23 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.
  • 17.  2015, Marcus Biel, http://www.marcus-biel.com/ ArrayList vs. LinkedList 0 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.
  • 18.  2015, Marcus Biel, http://www.marcus-biel.com/ ArrayList vs. LinkedList Implements Extends <<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.
  • 19.  2015, Marcus Biel, http://www.marcus-biel.com/ ArrayList vs. LinkedList Implements Extends <<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.
  • 20.  2015, Marcus Biel, http://www.marcus-biel.com/ LinkedList Implements Extends <<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.
  • 21.  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) {…} […] }
  • 22.  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.
  • 23.  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.
  • 24.  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.
  • 25.  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.
  • 26.  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.
  • 27.  2015, Marcus Biel, http://www.marcus-biel.com/ Doubly Linked List 23 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.
  • 28.  2015, Marcus Biel, http://www.marcus-biel.com/ Doubly Linked List 23 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.
  • 29.  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.
  • 30.  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.
  • 31.  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.
  • 32.  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.
  • 33.  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.
  • 34.  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:
  • 35.  2015, Marcus Biel, http://www.marcus-biel.com/ add element retrieve element retrieve and remove element Operations on a Queue add an element to the end of the Queue
  • 36.  2015, Marcus Biel, http://www.marcus-biel.com/ add element retrieve element retrieve and remove element Operations on a Queue retrieve an element from the front of the Queue, without removing it.
  • 37.  2015, Marcus Biel, http://www.marcus-biel.com/ add element retrieve element retrieve and remove element Operations on a Queue but of course the operation returns a reference to the object and does not copy it.
  • 38.  2015, Marcus Biel, http://www.marcus-biel.com/ add element retrieve element retrieve and remove element Operations on a Queue Okay. Finally you can retrieve and remove an element from the front of the Queue.
  • 39.  2015, Marcus Biel, http://www.marcus-biel.com/ Specific Events on a Queue In the lifetime of a Queue, there are special situations,
  • 40.  2015, Marcus Biel, http://www.marcus-biel.com/ Specific Events on a Queue ? like trying to remove an element… from an empty Queue
  • 41.  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
  • 42.  2015, Marcus Biel, http://www.marcus-biel.com/ return special value throw 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.
  • 43.  2015, Marcus Biel, http://www.marcus-biel.com/ return special value throw 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.
  • 44.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.Queue Throws 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.
  • 45.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements boolean add(E e) boolean offer(E e) A Queue allows to add elements to the end of the Queue.
  • 46.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements boolean 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.
  • 47.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements boolean add(E e) boolean offer(E e) LinkedList, like most Queue implementations, has an unlimited capacity, so it will never be full.
  • 48.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements boolean add(E e) boolean offer(E e) ArrayBlockingQueue on the other hand is a Queue implementation that has a limited capacity.
  • 49.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements E element() E peek() Next, “element” and “peek” allow you to retrieve an element from the front of the Queue, without removing it.
  • 50.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements E element() E peek() If the Queue is empty, the element function will throw an Exception, while peek() will return false.
  • 51.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve & remove elements E 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.
  • 52.  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.
  • 53.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.Deque <<interface>> Deque LinkedList Deque is the short form of “Double Ended Queue”
  • 54.  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.
  • 55.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.Deque throws Exception returns special value Add addFirst addLast offerFirst offerLast Retrieve getFirst getLast peekFirst peekFirst Retrieve & Remove removeFirst removeLas t pollFirst pollLast Just like a Queue, a Deque allows adding, retrieving and - retrieving and removing - an element.
  • 56.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.Deque throws Exception returns special value Add addFirst addLast offerFirst offerLast Retrieve getFirst getLast peekFirst peekFirst Retrieve & Remove removeFirst removeLas t pollFirst pollLast 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.
  • 57.  2015, Marcus Biel, http://www.marcus-biel.com/ java.util.Deque throws Exception returns special value Add addFirst addLast offerFirst offerLast Retrieve getFirst getLast peekFirst peekFirst Retrieve & Remove removeFirst removeLas t pollFirst pollLast Again, let’s look at this in more detail.
  • 58.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements You can add elements to both ends of the Deque.
  • 59.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements void addFirst(E e) Just like the add method of the Queue interface, addFirst
  • 60.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements void addFirst(E e) void addLast(E e) and addLast will throw an Exception when the Deque is full.
  • 61.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements 3 17 9 42 boolean offerFirst(E e) “offerFirst”…
  • 62.  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.
  • 63.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements boolean offerFirst(E e) boolean offerLast(E e) Please keep in mind that LinkedList has an unlimited capacity, so it will never be full.
  • 64.  2015, Marcus Biel, http://www.marcus-biel.com/ Add elements boolean 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.
  • 65.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements You can retrieve elements from both ends of the Deque, without removing them.
  • 66.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements E getFirst() “getFirst”…
  • 67.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements E getFirst() E getLast() and “getLast” will throw an Exception when the Queue is empty,
  • 68.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements E peekFirst() while “peekFirst”
  • 69.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements E peekFirst() E peekLast() and “peekLast” will return false in this case.
  • 70.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements Finally, you can retrieve and remove elements from both ends of the Deque.
  • 71.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve elements E removeFirst() “removeFirst”
  • 72.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve & remove elements E removeFirst() E removeLast() and “removeLast” will throw an Exception when the Queue is empty,
  • 73.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve & remove elements E pollFirst() while pollFirst
  • 74.  2015, Marcus Biel, http://www.marcus-biel.com/ Retrieve & remove elements E pollFirst() E pollLast() and pollLast will return false in this case.
  • 75.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack Okay. Now on to a completely different topic. The Deque interface also supports the methods of the Stack data structure, “push” “peek” and “pop”.
  • 76.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack Therefore java.util.LinkedList can also be used as Stack.
  • 77.  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.
  • 78.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack boolean push (E e) “push” adds an element to the top of the Stack.
  • 79.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack boolean push (E e) It is equivalent to the “addFirst” method.
  • 80.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack E peek() “peek” retrieves but does not remove an element from the top of the Stack.
  • 81.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack E peek() It is equivalent to the “peekFirst” method.
  • 82.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack E pop() “pop” retrieves and removes an element from the top of the Stack.
  • 83.  2015, Marcus Biel, http://www.marcus-biel.com/ Stack E pop() It is equivalent to the “removeFirst” method.
  • 84.  2015, Marcus Biel, http://www.marcus-biel.com/ Copyright © 2016 Marcus Biel All rights reserved

Editor's Notes

  • #2: Explore the Linked List data structure further, specifically focusing on the java.util.LinkedList class for more visit-
  • #3: In the previous ( http://www.slideshare.net/MarcusBiel/linked-listdata-structure ) episode I introduced you to the Linked List data structure.
  • #4: As the name implies, the Java class LinkedList is called LinkedList because internally it is based on a Doubly Linked List. 
  • #5: So what is the difference between the LinkedList data structure and the class java.util.LinkedList?
  • #6: As an analogy, think of the abstract concept of a car and a concrete car.
  • #7: The Linked List data structure is an abstract concept, independent of any specific programming language.
  • #8: The LinkedList Java class is a concrete implementation of this abstract concept. 
  • #9: So in this episode I will focus on one specific Linked List implementation, the java.util.LinkedList class.
  • #10: Among other interfaces, LinkedList implements the java.util.List interface.
  • #11: 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.
  • #12: In a previous tutorial (http://www.marcus-biel.com/arraylist/), I introduced you to the java.util.ArrayList class.
  • #13: As you can see, both classes implement the List interface which makes them somewhat similar. So what’s the difference between ArrayList and LinkedList?
  • #14: First of all, ArrayList ( http://www.marcus-biel.com/arraylist/ ) is based on an Array data structure,
  • #15: while LinkedList is based on a Doubly Linked List data structure.
  • #16: 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.
  • #17: 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.
  • #18: Otherwise, ArrayList might be a better choice, because storing elements in an array consumes less memory and generally gives faster access times.
  • #19: 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.
  • #20: In conclusion, there is no overall winner between ArrayList and LinkedList. Your specific requirements will determine which class to use.
  • #21: Let’s put ArrayList aside for now and have an in-depth look at the LinkedList implementation.
  • #22: Here is a simplified code excerpt from the java.util.LinkedList class.
  • #23: 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.
  • #24: The real source code is available online. After watching this episode, I recommend that you take a look at it for yourself.
  • #25: Okay. So, as you can see, LinkedList implements the List, Queue and Deque interfaces, as Deque extends the Queue interface.
  • #26: Next you can see that the LinkedList class has a reference to the first and the last elements of the list.
  • #27: Finally, you can see that the class has functions like get, add or remove - to access, insert or delete elements from the list.
  • #28: 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.
  • #29: 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.
  • #30: 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.
  • #31: As a user of the Collections class LinkedList, you never directly access the Nodes.
  • #32: Instead you use the public methods of the LinkedList class that internally operate on the private Node members.
  • #33: In the tutorial about ArrayList(http://www.marcus-biel.com/arraylist/). I introduced you to the methods of the List interface, so I won’t mention about those methods again.
  • #34: Instead, let’s go on and look at the methods of the Queue interface implemented by LinkedList.
  • #35: From a high level perspective, the Queue interface consists of three simple operations:
  • #36: add an element to the end of the Queue
  • #37: retrieve an element from the front of the Queue, without removing it.
  • #38: In the illustration the blue guy was copied, but of course the operation returns a reference to the object and does not copy it.
  • #39: Okay. Finally you can retrieve and remove an element from the front of the Queue.
  • #40: In the lifetime of a Queue, there are special situations,
  • #41: from an empty Queue
  • #42: or trying to add an element to a Queue that has a limited capacity and is currently full.
  • #43: Depending on your specific implementation, this might be an expected situation and you need a method that returns null or false in this case.
  • #44: Alternatively this might be an unexpected situation and you need a method that throws an Exception in this case.
  • #45: Therefore, 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. Okay, let’s look at this in more detail.
  • #46: A Queue allows to add elements to the end of the Queue.
  • #47: “add” will throw an Exception when the Queue is full, while “offer” will return false in this case.
  • #48: LinkedList, like most Queue implementations, has an unlimited capacity, so it will never be full.
  • #49: ArrayBlockingQueue on the other hand is a Queue implementation that has a limited capacity.
  • #50: Next, “element” and “peek” allow you to retrieve an element from the front of the Queue, without removing it.
  • #51: If the Queue is empty, the element function will throw an Exception, while peek() will return false.
  • #52: 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.
  • #53: Okay, now we will look at some methods of the Deque interface, as implemented by LinkedList.
  • #54: Deque is the short form of “Double Ended Queue”
  • #55: so it is a Queue that can be accessed from either end.
  • #56: Just like a Queue, a Deque allows adding, retrieving and - retrieving and removing - an element.
  • #57: 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.
  • #58: Again, let’s look at this in more detail.
  • #59: You can add elements to both ends of the Deque.
  • #60: Just like the add method of the Queue interface, addFirst
  • #61: and addLast will throw an Exception when the Deque is full.
  • #62: “offerFirst”…
  • #63: …and “offerLast” will return false instead of throwing an Exception.
  • #64: Please keep in mind that LinkedList has an unlimited capacity, so it will never be full.
  • #65: LinkedBlockingDeque on the other hand is a Deque implementation that may have a limited capacity. Okay, let’s go on.
  • #66: You can retrieve elements from both ends of the Deque, without removing them.
  • #67: “getFirst”…
  • #68: and “getLast” will throw an Exception when the Queue is empty,
  • #69: while “peekFirst”
  • #70: and “peekLast” will return false in this case.
  • #71: Finally, you can retrieve and remove elements from both ends of the Deque.
  • #72: “removeFirst”
  • #73: and “removeLast” will throw an Exception when the Queue is empty,
  • #74: while pollFirst
  • #75: and pollLast will return false in this case.
  • #76: Okay. Now on to a completely different topic. The Deque interface also supports the methods of the Stack data structure, “push” “peek” and “pop”.
  • #77: Therefore java.util.LinkedList can also be used as Stack.
  • #78: 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.
  • #79: “push” adds an element to the top of the Stack.
  • #80: It is equivalent to the “addFirst” method.
  • #81: “peek” retrieves but does not remove an element from the top of the Stack.
  • #82: It is equivalent to the “peekFirst” method.
  • #83: “pop” retrieves and removes an element from the top of the Stack.
  • #84: It is equivalent to the “removeFirst” method.