Presentation is loading. Please wait.

Presentation is loading. Please wait.

Marcus Biel, Software Craftsman

Similar presentations


Presentation on theme: "Marcus Biel, Software Craftsman"— Presentation transcript:

1 Marcus Biel, Software Craftsman http://www.marcus-biel.com
java.util.LinkedList Explore the Linked List data structure further, specifically focusing on the java.util.LinkedList class for more visit- Marcus Biel, Software Craftsman

2 Doubly Linked List 23 3 17 9 42 In the previous ( ) episode I introduced you to the Linked List data structure. In the previous episode I introduced you to the Linked List data structure.

3 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.  As the name implies, the Java class LinkedList is called LinkedList because internallyit is based on a Doubly Linked List. 

4 Concept vs. Implementation
So what is the difference between the LinkedList data structure and the class java.util.LinkedList? So what is the difference between the LinkedList data structure and the class java.util.LinkedList?

5 Concept vs. Implementation
As an analogy, think of the abstract concept of a car and a concrete car. As an analogy, think of the abstract concept of a car and a concrete car.

6 Concept vs. Implementation
The Linked List data structure is an abstract concept, independent of any specific programming language. The Linked List data structure is an abstract concept, independent of any specific programming language.

7 Concept vs. Implementation
The LinkedList Java class is a concrete implementation of this abstract concept.  The LinkedList Java class is a concrete implementation of this abstract concept. 

8 java.util.LinkedList So in this tutorial, I will focus on one specific
So in this episode I will focus on one specific Linked List implementation, the java.util.LinkedList class. Implements Extends So in this tutorial, I will focus on one specific Linked List implementation, the java.util.LinkedList class.

9 java.util.LinkedList Among other interfaces,
<<interface>> List Among other interfaces, LinkedList implements the java.util.List interface. LinkedList Implements Extends Among other interfaces, LinkedList implements the java.util.List interface.

10 java.util.LinkedList You can have duplicate elements in a List and
<<interface>> List 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. LinkedList Implements Extends 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 ArrayList vs. LinkedList
<<interface>> List In a previous tutorial ( I introduced you to the java.util.ArrayList class. ArrayList LinkedList Implements Extends In a previous tutorial, I introduced you to the java.util.ArrayList class.

12 ArrayList vs. LinkedList
<<interface>> List As you can see, both classes implement the List interface which makes them somewhat similar. So what’s the difference between ArrayList and LinkedList? ArrayList LinkedList Implements Extends 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 ArrayList vs. LinkedList
1 2 3 4 23 17 9 42 First of all, ArrayList ( ) is based on an Array data structure, First of all, ArrayList is based on an Array data structure,

14 ArrayList vs. LinkedList
1 2 3 4 23 17 9 42 while LinkedList is based on a Doubly Linked List data structure. 23 3 17 9 42 while LinkedList is based on a Doubly Linked List data structure.

15 ArrayList vs. LinkedList
1 2 3 4 23 17 9 42 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. 23 3 17 9 42 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 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. 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 ArrayList vs. LinkedList
1 2 3 4 23 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. Otherwise, ArrayList might be a better choice, because storing elements in an array consumes less memory and generally gives faster access times.

18 ArrayList vs. LinkedList
<<interface>> Collection 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. <<interface>> Queue Implements Extends <<interface>> List <<interface>> Deque ArrayList LinkedList 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 ArrayList vs. LinkedList
<<interface>> Collection In conclusion, there is no overall winner between ArrayList and LinkedList. Your specific requirements will determine which class to use. <<interface>> Queue Implements Extends <<interface>> List <<interface>> Deque ArrayList LinkedList In conclusion, there is no overall winner between ArrayList and LinkedList. Your specific requirements will determine which class to use.

20 LinkedList Let’s put ArrayList aside for now and
<<interface>> Collection Let’s put ArrayList aside for now and have an in-depth look at the LinkedList implementation. <<interface>> Queue Implements Extends <<interface>> List <<interface>> Deque LinkedList 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.
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) {…} […] } Here is a simplified code excerpt from the java.util.LinkedList class. Here is a simplified code excerpt from the java.util.LinkedList class.

22 LinkedList I don’t expect you to fully grasp every detail of the code,
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) {…} […] } 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. 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 LinkedList The real source code is available online.
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) {…} […] } The real source code is available online. After watching this episode, I recommend that you take a look at it for yourself. The real source code is available online. After finishing this presentation, I recommend that you take a look at it for yourself.

24 LinkedList So, as you can see, LinkedList implements the
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) {…} […] } Okay. So, as you can see, LinkedList implements the List, Queue and Deque interfaces, as Deque extends the Queue interface. So, as you can see, LinkedList implements the List, Queue and Deque interfaces, as Deque extends the Queue interface.

25 LinkedList Next you can see that the 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) {…} […] } Next you can see that the LinkedList class has a reference to the first and the last elements of the list. Next you can see that the LinkedList class has a reference to the first and the last elements of the list.

26 LinkedList Finally, you can see that the class has functions like-
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) {…} […] } Finally, you can see that the class has functions like get, add or remove - to access, insert or delete elements from 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.

27 Doubly Linked List 23 3 17 9 42 As we just saw in the code,
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. 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 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. 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 Node Here you see a code excerpt of a Node.
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; } } 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. 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 LinkedList As a user of the Collections class LinkedList,
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) {…} […] } As a user of the Collections class LinkedList, you never directly access the Nodes. As a user of the Collections class LinkedList, you never directly access the Nodes.

31 LinkedList Instead you use the public methods of the 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) {…} […] } Instead you use the public methods of the LinkedList class that internally operate on the private Node members. Instead you use the public methods of the LinkedList class that internally operate on the private Node members.

32 java.util.List In my tutorial about ArrayList ,
<<interface>> List In the tutorial about ArrayList( I introduced you to the methods of the List interface, so I won’t mention about those methods again. 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 java.util.Queue Instead, let’s go on and look at the methods of the
<<interface>> Queue Instead, let’s go on and look at the methods of the Queue interface implemented by LinkedList. LinkedList Instead, let’s go on and look at the methods of the Queue interface implemented by LinkedList.

34 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: end (tail) 23 3 17 9 42 front (head) From a high level perspective, the Queue interface consists of three simple operations:

35 add an element to the end of the Queue
Operations on a Queue add element retrieve element retrieve and remove element add an element to the end of the Queue add an element to the end of the Queue

36 retrieve an element from the front of the Queue,
Operations on a Queue add element retrieve element retrieve and remove element retrieve an element from the front of the Queue, without removing it. retrieve an element from the front of the Queue, without removing it.

37 but of course the operation returns a reference to the object
Operations on a Queue add element retrieve element retrieve and remove element In the illustration the blue guy was copied, but of course the operation returns a reference to the object and does not copy it. but of course the operation returns a reference to the object and does not copy it.

38 Operations on a Queue add element retrieve element
retrieve and remove element Okay. Finally you can retrieve and remove an element from the front of the Queue. Okay. Finally you can retrieve and remove an element from the front of the Queue.

39 Specific Events on a Queue
In the lifetime of a Queue, there are special situations, In the lifetime of a Queue, there are special situations,

40 Specific Events on a Queue
? from an empty Queue like trying to remove an element… from an empty Queue

41 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 or trying to add an element to a Queue that has a limited capacity and is currently full.

42 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. return special value throw Exception 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 Specific Events on a Queue
Alternatively this might be an unexpected situation and you need a method that throws an Exception in this case. return special value throw Exception Alternatively this might be an unexpected situation and you need a method that throws an Exception in this case.

44 java.util.Queue Throws Exception Returns Special Value Add add Offer Retrieve element Peek Retrieve & Remove Remove Poll 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. 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 A Queue allows to add elements to the end of the Queue.
boolean add(E e) boolean offer(E e) A Queue allows to add elements to the end of the Queue. A Queue allows to add elements to the end of the Queue.

46 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. “add” will throw an Exception when the Queue is full, while “offer” will return false in this case.

47 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. LinkedList, like most Queue implementations, has an unlimited capacity, so it will never be full.

48 Add elements boolean add(E e) boolean offer(E e)
ArrayBlockingQueue on the other hand is a Queue implementation that has a limited capacity. ArrayBlockingQueue on the other hand is a Queue implementation that has a limited capacity.

49 Retrieve elements E element() E peek() Next, “element” and “peek”
Next, “element” and “peek” allow you to retrieve an element from the front of the Queue, without removing it. Next, “element” and “peek” allow you to retrieve an element from the front of the Queue, without removing it.

50 Retrieve elements E element() E peek() If the Queue is empty,
If the Queue is empty, the element function will throw an Exception, while peek() will return false. If the Queue is empty, the element function will throw an Exception, while peek() will return false.

51 Retrieve & remove elements
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. 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 java.util.Deque <<interface>> Deque Okay, now we will look at some methods of the Deque interface, as implemented by LinkedList. LinkedList 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”
java.util.Deque <<interface>> Deque Deque is the short form of “Double Ended Queue” LinkedList Deque is the short form of “Double Ended Queue”

54 so it is a Queue that can be accessed from either end.
java.util.Deque <<interface>> Deque so it is a Queue that can be accessed from either end. LinkedList so it is a Queue that can be accessed from either end.

55 java.util.Deque Just like a Queue, a Deque allows adding, retrieving
throws Exception returns special value Add addFirst
addLast offerFirst
offerLast Retrieve getFirst
getLast peekFirst
peekFirst Retrieve & Remove removeFirst
removeLast pollFirst
pollLast Just like a Queue, a Deque allows adding, retrieving and - retrieving and removing - an element. Just like a Queue, a Deque allows adding, retrieving and - retrieving and removing - an element.

56 java.util.Deque But as it can be accessed from either end,
throws Exception returns special value Add addFirst
addLast offerFirst
offerLast Retrieve getFirst
getLast peekFirst
peekFirst Retrieve & Remove removeFirst
removeLast 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. 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.
java.util.Deque throws Exception returns special value Add addFirst
addLast offerFirst
offerLast Retrieve getFirst
getLast peekFirst
peekFirst Retrieve & Remove removeFirst
removeLast pollFirst
pollLast Again, let’s look at this in more detail. 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
Add elements Just like the add method of the Queue interface, addFirst void addFirst(E e) Just like the add method of the Queue interface, addFirst

60 and addLast will throw an Exception when the Deque is full.
Add elements and addLast will throw an Exception when the Deque is full. void addFirst(E e) void addLast(E e) and addLast will throw an Exception when the Deque is full.

61 Add elements boolean offerFirst(E e) 3 17 9 42 “offerFirst”…

62 …and “offerLast” will return false instead of throwing an Exception.
Add elements 3 17 9 22 …and “offerLast” will return false instead of throwing an Exception. boolean offerFirst(E e) boolean offerLast(E e) …and “offerLast” will return false instead of throwing an Exception.

63 Please keep in mind that LinkedList has an unlimited capacity,
Add elements Please keep in mind that LinkedList has an unlimited capacity, so it will never be full. 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 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. 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 You can retrieve elements from both ends of the Deque,
You can retrieve elements from both ends of the Deque, without removing them. You can retrieve elements from both ends of the Deque, without removing them.

66 Retrieve elements “getFirst”… E getFirst() “getFirst”…

67 and “getLast” will throw an Exception when the Queue is empty,
Retrieve elements and “getLast” will throw an Exception when the Queue is empty, E getFirst() E getLast() and “getLast” will throw an Exception when the Queue is empty,

68 Retrieve elements while “peekFirst” E peekFirst() while “peekFirst”

69 and “peekLast” will return false in this case.
Retrieve elements and “peekLast” will return false in this case. E peekFirst() E peekLast() and “peekLast” will return false in this case.

70 you can retrieve and remove elements from both ends of the Deque.
Retrieve elements Finally, you can retrieve and remove elements from both ends of the Deque. Finally, you can retrieve and remove elements from both ends of the Deque.

71 Retrieve elements “removeFirst” E removeFirst() “removeFirst”

72 Retrieve & remove elements
and “removeLast” will throw an Exception when the Queue is empty, E removeFirst() E removeLast() and “removeLast” will throw an Exception when the Queue is empty,

73 Retrieve & remove elements
while pollFirst E pollFirst() while pollFirst

74 Retrieve & remove elements
and pollLast will return false in this case. E pollFirst() E pollLast() and pollLast will return false in this case.

75 Stack Okay. Now on to a completely different topic.
Okay. Now on to a completely different topic. The Deque interface also supports the methods of the Stack data structure, “push” “peek” and “pop”. 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 Stack A Stack is a very simple data structure,
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. 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.
boolean push (E e) “push” adds an element to the top of the Stack.

79 It is equivalent to the “addFirst” method.
Stack It is equivalent to the “addFirst” method. boolean push (E e) It is equivalent to the “addFirst” method.

80 but does not remove an element from the top of the Stack.
“peek” retrieves but does not remove an element from the top of the Stack. E peek() “peek” retrieves but does not remove an element from the top of the Stack.

81 It is equivalent to the “peekFirst” method.
Stack It is equivalent to the “peekFirst” method. E peek() It is equivalent to the “peekFirst” method.

82 “pop” retrieves and removes an element from the top of the Stack.
E pop() “pop” retrieves and removes an element from the top of the Stack.

83 It is equivalent to the “removeFirst” method.
Stack It is equivalent to the “removeFirst” method. E pop() It is equivalent to the “removeFirst” method.

84 Copyright © 2016 Marcus Biel All rights reserved


Download ppt "Marcus Biel, Software Craftsman"

Similar presentations


Ads by Google