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

What's hot

Java Collections
Java  Collections Java  Collections
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
babak danyal
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
Manvendra Singh
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Java platform
Java platformJava platform
Java platform
BG Java EE Course
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
Surendar Meesala
 
Introduction to Spring's Dependency Injection
Introduction to Spring's Dependency InjectionIntroduction to Spring's Dependency Injection
Introduction to Spring's Dependency Injection
Richard Paul
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
Muthukumaran Subramanian
 
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
Maven Basics - Explained
Maven Basics - ExplainedMaven Basics - Explained
Maven Basics - Explained
Smita Prasad
 
Data stage
Data stageData stage
Data stage
Sai Kiran
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentationJohn Slick
 
Second Level Cache in JPA Explained
Second Level Cache in JPA ExplainedSecond Level Cache in JPA Explained
Second Level Cache in JPA Explained
Patrycja Wegrzynowicz
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
Knoldus Inc.
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
Archana Gopinath
 

What's hot (20)

Java Collections
Java  Collections Java  Collections
Java Collections
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java platform
Java platformJava platform
Java platform
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Introduction to Spring's Dependency Injection
Introduction to Spring's Dependency InjectionIntroduction to Spring's Dependency Injection
Introduction to Spring's Dependency Injection
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Maven Basics - Explained
Maven Basics - ExplainedMaven Basics - Explained
Maven Basics - Explained
 
String Handling
String HandlingString Handling
String Handling
 
Data stage
Data stageData stage
Data stage
 
Java exception
Java exception Java exception
Java exception
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Second Level Cache in JPA Explained
Second Level Cache in JPA ExplainedSecond Level Cache in JPA Explained
Second Level Cache in JPA Explained
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 

Viewers also liked

Singly link list
Singly link listSingly link list
Singly link list
Rojin Khadka
 
Array List
Array ListArray List
Single linked list
Single linked listSingle linked list
Single linked list
jasbirsingh chauhan
 
Link List
Link ListLink List
Link List
umiekalsum
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
José Paumard
 
Linked to ArrayList: the full story
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full story
José Paumard
 
Linked list
Linked listLinked list
Linked list
akshat360
 

Viewers also liked (7)

Singly link list
Singly link listSingly link list
Singly link list
 
Array List
Array ListArray List
Array List
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Link List
Link ListLink List
Link List
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 
Linked to ArrayList: the full story
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full story
 
Linked list
Linked listLinked list
Linked list
 

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

Linked List data structure
Linked List data structureLinked List data structure
Linked List data structure
Marcus Biel
 
Java ArrayList Video Tutorial
Java ArrayList Video TutorialJava ArrayList Video Tutorial
Java ArrayList Video Tutorial
Marcus Biel
 
Java Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialJava Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video Tutorial
Marcus Biel
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
Naga Muruga
 
Java Collections
Java CollectionsJava Collections
Java Collections
rithustutorials
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
karymadelaneyrenne19
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
arpitaeron555
 
Making Steaks from Sacred Cows
Making Steaks from Sacred CowsMaking Steaks from Sacred Cows
Making Steaks from Sacred Cows
Kevlin Henney
 
Linked Lists in Python, Python Institute in Delhi.pdf
Linked Lists in Python, Python Institute in Delhi.pdfLinked Lists in Python, Python Institute in Delhi.pdf
Linked Lists in Python, Python Institute in Delhi.pdf
ESS Institute
 
Unit 3 lecture-2
Unit 3 lecture-2Unit 3 lecture-2
Unit 3 lecture-2
vishal choudhary
 
Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
Vibrant Technologies & Computers
 
Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan
 
LinkedList vs ArrayList in Java | Edureka
LinkedList vs ArrayList in Java | EdurekaLinkedList vs ArrayList in Java | Edureka
LinkedList vs ArrayList in Java | Edureka
Edureka!
 
OpenDMS - the first 2 weeks
OpenDMS - the first 2 weeksOpenDMS - the first 2 weeks
OpenDMS - the first 2 weeks
JPC Hanson
 
Beginning linq
Beginning linqBeginning linq
Beginning linq
Shikha Gupta
 
Lesson 5 link list
Lesson 5  link listLesson 5  link list
Lesson 5 link list
MLG College of Learning, Inc
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
Nimmi Weeraddana
 
3 tier architecture in asp.net
3 tier architecture in asp.net3 tier architecture in asp.net
3 tier architecture in asp.net
Ravi Bansal
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
Mahmoud Ouf
 

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

Linked List data structure
Linked List data structureLinked List data structure
Linked List data structure
 
Java ArrayList Video Tutorial
Java ArrayList Video TutorialJava ArrayList Video Tutorial
Java ArrayList Video Tutorial
 
Java Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialJava Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video Tutorial
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Making Steaks from Sacred Cows
Making Steaks from Sacred CowsMaking Steaks from Sacred Cows
Making Steaks from Sacred Cows
 
Linked Lists in Python, Python Institute in Delhi.pdf
Linked Lists in Python, Python Institute in Delhi.pdfLinked Lists in Python, Python Institute in Delhi.pdf
Linked Lists in Python, Python Institute in Delhi.pdf
 
Unit 3 lecture-2
Unit 3 lecture-2Unit 3 lecture-2
Unit 3 lecture-2
 
Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
 
Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2
 
LinkedList vs ArrayList in Java | Edureka
LinkedList vs ArrayList in Java | EdurekaLinkedList vs ArrayList in Java | Edureka
LinkedList vs ArrayList in Java | Edureka
 
OpenDMS - the first 2 weeks
OpenDMS - the first 2 weeksOpenDMS - the first 2 weeks
OpenDMS - the first 2 weeks
 
Beginning linq
Beginning linqBeginning linq
Beginning linq
 
Lesson 5 link list
Lesson 5  link listLesson 5  link list
Lesson 5 link list
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 
3 tier architecture in asp.net
3 tier architecture in asp.net3 tier architecture in asp.net
3 tier architecture in asp.net
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 

Recently uploaded

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

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

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