In this keynote I give you and in-depth look at the java.utilLinkedList class.
I compare LinkedList to ArrayList and I explain the most important methods of the interface java.util.Queue and java.util.Deque that LinkedList implements. Last but not least, I show you how you can use LinkedList as a Stack data structure.
Java 8 Stream API. A different way to process collections.David Gómez García
A look on one of the features of Java 8 hidden behind the lambdas. A different way to iterate Collections. You'll never see the Collecions the same way.
These are the slides I used on my talk at the "Tech Thursday" by Oracle in June in Madrid.
Presentation provides introduction and detailed explanation of the Java 8 Lambda and Streams. Lambda covers with Method references, default methods and Streams covers with stream operations,types of streams, collectors. Also streams are elaborated with parallel streams and benchmarking comparison of sequential and parallel streams.
Additional slides are covered with Optional, Splitators, certain projects based on lambda and streams
Java is Object Oriented Programming. Java 8 is the latest version of the Java which is used by many companies for the development in many areas. Mobile, Web, Standalone applications.
Java 8 Stream API. A different way to process collections.David Gómez García
A look on one of the features of Java 8 hidden behind the lambdas. A different way to iterate Collections. You'll never see the Collecions the same way.
These are the slides I used on my talk at the "Tech Thursday" by Oracle in June in Madrid.
Presentation provides introduction and detailed explanation of the Java 8 Lambda and Streams. Lambda covers with Method references, default methods and Streams covers with stream operations,types of streams, collectors. Also streams are elaborated with parallel streams and benchmarking comparison of sequential and parallel streams.
Additional slides are covered with Optional, Splitators, certain projects based on lambda and streams
Java is Object Oriented Programming. Java 8 is the latest version of the Java which is used by many companies for the development in many areas. Mobile, Web, Standalone applications.
This is a beginner's guide to Java 8 Lambdas, accompnied with executable code examples which you can find at https://github.com/manvendrasinghkadam/java8streams. Java 8 Streams are based on Lambdas, so this presentation assumes you know Lambdas quite well. If don't then please let me know I'll create another presentation regarding it with code examples. Lambdas are relatively easy to use and with the power of stream api you can do functional programming in Java right from start. This is very cool to be a Java programmer now.
This ppt gives a general idea about the multithreading concepts in the java programming language. hope you find it useful
P.S :
sorry there is a correction in one of the slides
where i have entered implements thread
it is wrong it is actually implements Runnable
thank you!
Slides for a lightning talk on Java 8 lambda expressions I gave at the Near Infinity (www.nearinfinity.com) 2013 spring conference.
The associated sample code is on GitHub at https://github.com/sleberknight/java8-lambda-samples
This is a beginner's guide to Java 8 Lambdas, accompnied with executable code examples which you can find at https://github.com/manvendrasinghkadam/java8streams. Java 8 Streams are based on Lambdas, so this presentation assumes you know Lambdas quite well. If don't then please let me know I'll create another presentation regarding it with code examples. Lambdas are relatively easy to use and with the power of stream api you can do functional programming in Java right from start. This is very cool to be a Java programmer now.
This ppt gives a general idea about the multithreading concepts in the java programming language. hope you find it useful
P.S :
sorry there is a correction in one of the slides
where i have entered implements thread
it is wrong it is actually implements Runnable
thank you!
Slides for a lightning talk on Java 8 lambda expressions I gave at the Near Infinity (www.nearinfinity.com) 2013 spring conference.
The associated sample code is on GitHub at https://github.com/sleberknight/java8-lambda-samples
The slides of my JavaOne 2016 talk. This talk is a tutorial on how to write lambda expressions, how to compose them using default methods in functional interfaces, and how to create factory methods in those interfaces. Many examples and patterns are provided.
The slides of my university talk, Devoxx Belgium 2016.
The goal of this talk is to compare the two most popular implementations of List: LinkedList and ArrayList, and provide hints on which one to use in what case.
In this presentation you will learn everything you need to know about ArrayList. I explain the difference of capacity and size, and I introduce you to the methods of the Java ArrayList class, divided by the interfaces java.util.Collection and java.util.List.
Java Collections Framework Inroduction with Video TutorialMarcus Biel
This presentation is part of my free Java 8 course focusing on clean code principles. In this piece, you will be given a high-level introduction of the Java Collections Framework (JCF).
You can find the full article with lots of other additional materials here -
http://www.marcus-biel.com/java-collections-framework/
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
Abstract Data Types
(a) Explain briefly what is meant by the term abstract data type (ADT). Give two
reasons why use of ADTs is good programming practice.
(b) Write out a signature, or interface, that defines the operations of a stack ADT.
(c) Consider a string of characters of the form
... (.( ... ).) ...
where ... indicates an arbitrary sequence of characters (except for parentheses),
(.( indicates an arbitrary number (one or more) of opening parentheses, and
similarly ).) indicates an arbitrary number of closing parentheses.
Using only the stack abstraction operations defined above, write pseudocode for
an algorithm that determines, using a stack, whether or not the number of closing
parentheses is the same as the number of opening parentheses.
You may assume the existence of a function read(str,ch) that reads the next character
of string str into ch.
You may also assume that you can invoke a function reportFail, that will cause
termination with failure, and similarly, reportSuccess causes termination with a
success indication.
Further, you may also assume that you can call a function newStack(S) to create
a new empty stack S, and eos(str) that returns false when you reach the end of
the string.
Solution
(a) Explain briefly what is meant by the term abstract data type (ADT). Give two
reasons why use of ADTs is good programming practice.
A data type is a collection of values and a set of operations on those values. That collection and
these operations form a mathematical construct that may be implemented with the use of a
particular hardware or software data structure. The term abstract data type (ADT) refers to the
basic mathematical concept that defines the data type. We have discussed four different
implementations of the list data structure.
In case of implementation of the list with the use of an array, the size of the array gives difficulty
if increased.
To avoid this, we allocate memory dynamically for nodes before connecting these nodes with the
help of pointers.
For this purpose, we made a singly linked list and connected it with the next pointer to make a
chain.
Moving forward is easy but going back is a difficult task.
To overcome this problem, we made a doubly linked list using prev andnext pointers. With the
help of these pointers, we can move forward and backward very easily. Now we face another
problem that the prev pointer of first node and the next pointer of the last node are NULL.
Therefore, we have to be careful in case of NULL pointers. To remove the NULL pointers, we
made the circular link list by connecting the first and last node.
The program employing the list data structure is not concerned with its implementation.
We do not care how the list is being implemented whether through an array, singly linked list,
doubly linked list or circular linked list. It has been witnessed that in these four implementations
of the list, the interface remained the same i.e. it implements the same methods like add, get,
next, start a.
Introduction and BackgroundIn recent lectures we discussed usi.pdfarpitaeron555
Introduction and Background
In recent lectures we discussed using arrays, classes and interfaces (see newly added course
notes if you want to read ahead about interfaces – we will cover them this week in lecture). In
this lab you will utilize all of these topics to build a simple yet useful new class. Consider the
following interface describing the methods for a simple double ended queue (or deque):
public interface SimpleDeque
{
public void addFront(Object X); // Add Object X at front of list
public void addRear(Object X); // Add Object X at rear of list
// If array is full, add methods should do nothing
public Object removeFront(); // Remove and return Object X from
// front of list
public Object removeRear(); // Remove and return Object X from
// rear of list
// If array is empty, remove methods should return null
public boolean isEmpty(); // Return true if the list is empty
// Return false otherwise
}
A queue has the behavior such that items are added at the rear and removed from the front,
thereby giving a First In First Out (FIFO) access to the items added and subsequently removed
from the list. No other manipulations of the data are permitted (for example, we cannot add or
remove anywhere in the middle). Looking at it \"in reverse\", we could add new items at the
front of the queue and remove them from the rear. This is still providing FIFO access, but just
from a different point of view. Now consider both adding and removing items at the rear of the
list (without ever accessing the front). This is called stack access and gives us Last In First Out
(LIFO) access to the items (the data is removed in reverse order). The same behavior occurs if
we both add and remove at the front without ever accessing the rear of the list.
The simple deque above is expressed as an interface rather than a class, because we are not
describing the data or how it is represented -- we are simply describing its access behavior.
However, to actually build a working deque, we need a class that implements the interface
above. For example:
public class MyDeque implements SimpleDeque
{
Object [] theData;
int numItems;
public MyDeque(int maxItems)
{
theData = new Object[maxItems];
numItems = 0;
}
// Implementation of the five methods of SimpleDeque, plus
// perhaps other methods as well
}
Note that the implementation above uses an array of Object to store the items in the deque. Since
Object is the base class to all other Java classes, an array of Object can thus be used to store any
Java class types (we can even store primitive values if we utilize their wrapper classes). Also
note that nothing in the SimpleDeque interface requires an array to be used to store the data. You
will see in your CS 0445 course that a linked list may in fact be a better implementation than an
array in this case. However, for this implementation we will use an array because it is simple and
easy to understand.
Another important thing to notice about the partial implementation above is tha.
Presented at JavaZone (10th September 2014)
Video available at https://vimeo.com/105758303
But how much reason supports the rituals and mantras often repeated as coding guidelines? It turns out that the advice often fails, even for the novices they are intended to guide. Let's reason through these rather than accept them as unquestioned habits.
How many asserts should a test case have or not have? How much work should a constructor (not) do? What mantra guides test-first programming? How do you name your classes and other identifiers? How do you lay out your code? These questions and others have standard answers based on received and repeated mantras, practices that are communicated in good faith to be passed on as habits. But how much reason supports these assertions? It turns out that the advice often fails, even for the novices they are intended to guide.
This talk has little respect for ritual and tradition and takes no prisoners: What actually makes sense and what doesn't when it comes to matters of practice? What guidelines offer the greatest effect and the greatest learning?
core & advanced java classes in Mumbai
best core & advanced java classes in Mumbai with job assistance.
our features are:
expert guidance by it industry professionals
lowest fees of 5000
practical exposure to handle projects
well equiped lab
after course resume writing guidance
YouTube Link: https://youtu.be/Iq9tUK-6J78
** Java Certification Training: https://www.edureka.co/java-j2ee-training-course **
This Edureka PPT on “LinkedList vs ArrayList in Java” will talk about the differences between LinkedList and ArrayList in Java. Through this PPT, you will learn the following topics:
What is a LinkedList?
What is an ArrayList?
Similarities between LinkedList and ArrayList
Differences between LinkedList and ArrayList
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Castbox: https://castbox.fm/networks/505?country=in
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
This presentation provides a briefing on how to upload submissions and documents in Google Classroom. It was prepared as part of an orientation for new Sainik School in-service teacher trainees. As a training officer, my goal is to ensure that you are comfortable and proficient with this essential tool for managing assignments and fostering student engagement.
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
Biological screening of herbal drugs: Introduction and Need for
Phyto-Pharmacological Screening, New Strategies for evaluating
Natural Products, In vitro evaluation techniques for Antioxidants, Antimicrobial and Anticancer drugs. In vivo evaluation techniques
for Anti-inflammatory, Antiulcer, Anticancer, Wound healing, Antidiabetic, Hepatoprotective, Cardio protective, Diuretics and
Antifertility, Toxicity studies as per OECD guidelines
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
A Strategic Approach: GenAI in EducationPeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
Embracing GenAI - A Strategic ImperativePeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
Read| The latest issue of The Challenger is here! We are thrilled to announce that our school paper has qualified for the NATIONAL SCHOOLS PRESS CONFERENCE (NSPC) 2024. Thank you for your unwavering support and trust. Dive into the stories that made us stand out!
The French Revolution, which began in 1789, was a period of radical social and political upheaval in France. It marked the decline of absolute monarchies, the rise of secular and democratic republics, and the eventual rise of Napoleon Bonaparte. This revolutionary period is crucial in understanding the transition from feudalism to modernity in Europe.
For more information, visit-www.vavaclasses.com
Palestine last event orientationfvgnh .pptxRaedMohamed3
An EFL lesson about the current events in Palestine. It is intended to be for intermediate students who wish to increase their listening skills through a short lesson in power point.
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.
Explore the Linked List data structure further, specifically focusing on the java.util.LinkedList class for more visit-
In the previous ( http://www.slideshare.net/MarcusBiel/linked-listdata-structure ) episode I introduced you to the Linked List data structure.
As the name implies, the Java class LinkedList is called LinkedList because internally it is based on a Doubly Linked List.
So what is the difference between the LinkedList data structure and the class java.util.LinkedList?
As an analogy, think of the abstract concept of a car and a concrete car.
The Linked List data structure is an abstract concept, independent of any specific programming language.
The LinkedList Java class is a concrete implementation of this abstract concept.
So in this episode I will focus on one specific Linked List implementation, the java.util.LinkedList class.
Among other interfaces, LinkedList implements the java.util.List interface.
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.
In a previous tutorial (http://www.marcus-biel.com/arraylist/), I introduced you to the java.util.ArrayList class.
As you can see, both classes implement the List interface which makes them somewhat similar. So what’s the difference between ArrayList and LinkedList?
First of all, ArrayList ( http://www.marcus-biel.com/arraylist/ ) is based on an Array data structure,
while LinkedList is based on a Doubly Linked List data structure.
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.
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.
Otherwise, ArrayList might be a better choice, because storing elements in an array consumes less memory and generally gives faster access times.
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.
In conclusion, there is no overall winner between ArrayList and LinkedList. Your specific requirements will determine which class to use.
Let’s put ArrayList aside for now and have an in-depth look at the LinkedList implementation.
Here is a simplified code excerpt from the java.util.LinkedList class.
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.
The real source code is available online. After watching this episode, I recommend that you take a look at it for yourself.
Okay. So, as you can see, LinkedList implements the List, Queue and Deque interfaces, as Deque extends the Queue interface.
Next you can see that the LinkedList class has a reference to the first and the last elements of the list.
Finally, you can see that the class has functions like get, add or remove - to access, insert or delete elements from the list.
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.
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.
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.
As a user of the Collections class LinkedList, you never directly access the Nodes.
Instead you use the public methods of the LinkedList class that internally operate on the private Node members.
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.
Instead, let’s go on and look at the methods of the Queue interface implemented by LinkedList.
From a high level perspective, the Queue interface consists of three simple operations:
add an element to the end of the Queue
retrieve an element from the front of the Queue, without removing it.
In the illustration the blue guy was copied, but of course the operation returns a reference to the object and does not copy it.
Okay. Finally you can retrieve and remove an element from the front of the Queue.
In the lifetime of a Queue, there are special situations,
from an empty Queue
or trying to add an element to a Queue that has a limited capacity and is currently full.
Depending on your specific implementation, this might be an expected situation and you need a method that returns null or false in this case.
Alternatively this might be an unexpected situation and you need a method that throws an Exception in this case.
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.
A Queue allows to add elements to the end of the Queue.
“add” will throw an Exception when the Queue is full, while “offer” will return false in this case.
LinkedList, like most Queue implementations, has an unlimited capacity, so it will never be full.
ArrayBlockingQueue on the other hand is a Queue implementation that has a limited capacity.
Next, “element” and “peek” allow you to retrieve an element from the front of the Queue, without removing it.
If the Queue is empty, the element function will throw an Exception, while peek() will return false.
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.
Okay, now we will look at some methods of the Deque interface, as implemented by LinkedList.
Deque is the short form of “Double Ended Queue”
so it is a Queue that can be accessed from either end.
Just like a Queue, a Deque allows adding, retrieving and - retrieving and removing - an element.
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.
Again, let’s look at this in more detail.
You can add elements to both ends of the Deque.
Just like the add method of the Queue interface, addFirst
and addLast will throw an Exception when the Deque is full.
“offerFirst”…
…and “offerLast” will return false instead of throwing an Exception.
Please keep in mind that LinkedList has an unlimited capacity, so it will never be full.
LinkedBlockingDeque on the other hand is a Deque implementation that may have a limited capacity. Okay, let’s go on.
You can retrieve elements from both ends of the Deque, without removing them.
“getFirst”…
and “getLast” will throw an Exception when the Queue is empty,
while “peekFirst”
and “peekLast” will return false in this case.
Finally, you can retrieve and remove elements from both ends of the Deque.
“removeFirst”
and “removeLast” will throw an Exception when the Queue is empty,
while pollFirst
and pollLast will return false in this case.
Okay. Now on to a completely different topic. The Deque interface also supports the methods of the Stack data structure, “push” “peek” and “pop”.
Therefore java.util.LinkedList can also be used as 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.
“push” adds an element to the top of the Stack.
It is equivalent to the “addFirst” method.
“peek” retrieves but does not remove an element from the top of the Stack.
It is equivalent to the “peekFirst” method.
“pop” retrieves and removes an element from the top of the Stack.