L15 ListsStacksQueues A (ch20)
L15 ListsStacksQueues A (ch20)
Acknowledgement: The slides mainly rely on the textbook of Y. D. Liang titled “Introduction to
Java Programming, 10th Ed.”, Pearson Edu. Inc. COSC 121. Page 1
Previous Lecture
▪ Intro to the implementation of ArrayLists
▪ Intro to Generics
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 2
Outline
Today:
▪ Collection Interface
▪ Lists:
• ArrayList (again)
• LinkedList
• ArrayList vs LinkedList
• Big-Oh Notation
Next lecture:
▪ More Lists: Vector / Stack
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 6
Remember: Collections Class Hierarchy
This diagram is not accurate. There are several abstract classes
and interfaces missing, but it should help in visualization
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 7
Collection
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 9
The Collection Interface
The Collection interface is the root interface for manipulating
a collection of objects. It declares the methods for:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 10
The Collection Interface
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 11
Example on Collection
ArrayList<String> c1 = new ArrayList<>(Arrays.asList("A","B","A","D"));
ArrayList<String> c2 = new ArrayList<>(Arrays.asList("A","X","Y","Z"));
System.out.println("c1: " + c1);
System.out.println("c2: " + c2); c1 A B A D
//addAll
c3 = (ArrayList<String>) c1.clone(); //restore c1 items
c3.addAll(c2); //add all c2 items to c3. Duplicates are allowed
System.out.println("Items in c1 and c2 combined: " + c3); //[A,B,A,D,A,X,Y,Z]
//removeAll
c3 = (ArrayList<String>) c1.clone(); //restore c1 items
c3.removeAll(c2); //remove all c2 items from c3
System.out.println("Items in c1 but not in c2: " + c3); //[B,D]
//retainAll
c3 = (ArrayList<String>) c1.clone(); //restore c1 items
c3.retainAll(c2); //keep items in c3 that are also in c1
System.out.println("Items in c1 (intersection) c2: " + c3); //[A,A]
COSC 121. Page 12
Lists
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 14
List Interface
The List interface
▪ Ordered (each element has an index)
▪ Duplicates allowed.
Methods:
▪ All methods from Collection
▪ & iterator methods: listIterator()
▪ & index-based methods
• Adding add(index,element)
addAll(index, collection)
• Removing remove(index)
• Setting set(index, element)
• Getting get(index)
• Checking indexOf(element)
lastIndexOf(element)
• Sublist subList(fromIndex, toIndex)
Concrete classes
▪ ArrayList, LinkedList
ArrayList<E>
Methods:
▪ All methods from List
▪ & extra method
• trimToSize() Trims the capacity of this ArrayList instance
to be the list's current size.
Constructors
▪ ArrayList()
• Creates an empty list with default capacity.
▪ ArrayList(int initialCapacity)
▪ ArrayList(c: Collection<? extends E>)
• Create ArrayList from an existing collection of any subtype of E
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 16
LinkedList<E>
Methods:
▪ All methods from List
▪ & extra methods specifically for first and last elements
• Adding addFirst(), addLast()
• Removing removeFirst(), removeLast()
• Getting getFirst(), getLast()
Constructors
▪ LinkedList()
• Creates a default empty list
▪ LinkedList(c: Collection<? extends E>)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 17
Initializing a List as We Create it
In Java 9+, all of the following is valid:
B) To create a general list (i.e. can only use methods from List):
List<Integer> y = Arrays.asList(1,2,3);//y is modifiable
List<Integer> x = List.of(1,2,3); //x is unmodifiable
b) list1.remove(list2)?
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 20
When to use which?
In order to answer this question, we need to learn a few things
first about ArrayLists vs LinkedLists, namely:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 21
Remember: Structure of an ArrayList
An array list is implemented using an array (a fixed-size data
structure). Whenever the current array cannot hold new elements
in the list, a larger new array is created to replace the current
array. Arrays use an INDEX to reference its elements
memory address
0
1 int[] arr = new int[6];
2
… . arr [index] = arr address + index
arr .
index 0 element 0 arr[0] = arr address Assuming
.
1 element 1 . arr[1] = arr address + 1 1 int = 1
Consecutive
Memory Space … … . memory
. block
Reserved 5 element 5 arr[5] = arr address + 5
.
Illustration:
▪ http://cs.armstrong.edu/liang/animation/web/ArrayList.html
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 22
Basic Structure of a LinkedList, cont.
Each data element is contained in an object, called the node.
When a new element is added to the list, a node is created to
contain it.
Each node is linked to its next neighbor.
A variable head refers to the first node, and a variable tail to the
last node.
▪ If the list is empty, both head and tail are null.
head tail
Illustrations:
▪ http://cs.armstrong.edu/liang/animation/web/LinkedList.html
▪ https://visualgo.net/en/list COSC 121. Page 24
Others types of Linked Lists
Circular Linked List
head tail
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 25
Others types of Linked Lists, cont.
Doubly Linked List
▪ This is the type that Java 1.7 uses for LinkedList class
▪ Can be traversed in both directions
head tail
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 26
Others types of Linked Lists, cont.
Circular Doubly Linked List
head tail
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 27
When to use which (again)?
In general, use either one if you need access through an index.
However, note the following (based on the table on the next slide):
▪ ArrayList:
• Efficient for getting/setting elements by an index
• Good for Inserting/removing elements near end (but not beginning)
• Access elements using “Random Access” (good)
▪ LinkedList:
• Efficient for inserting/removing elements near beginning and end.
• Efficient when using iterators.
• Access elements using “Sequential Access” (bad)
• hence it is not good for getting/setting elements away from beginning/end
In big-Oh notation:
▪ The performance is specified as a function of n which is the
size of the problem.
• e.g. n may be the size of an array, or the number of values to compute
▪ Only the most significant expression of n is chosen:
• e.g. If the method performs n3 + n2 + n steps, it is O(n3).
• Significance ordering: 2n , n5 , n4 , n3 , n2 , n*log(n) , n , log(n)
▪ Constants are ignored for big-Oh:
• e.g. If the method performs 5*n3 + 4*n2 steps, it is O(n3).
• This is because we measure the growth rate, not the number of
executions, and hence both n and 10n will grow at the same rate.
COSC 121. Page 31
Common Big-Oh Notation Values
There are certain classes of functions with common names:
▪ constant = O(1)
▪ logarithmic = O(log n)
▪ linear = O(n)
▪ quadratic = O(n2)
▪ exponential = O(2n)
100
90
80
O(1)
70
60 O(logN)
50 O(N)
40 2
O(N )
30 N
20 O(2 )
10
0
System.out.print(a[16]);
O(1)
System.out.print(a[n-1]);
Q: Is this the
best way?
Experiment: Printing all elements in reverse order
Experiment (based on the previous slide):
▪ For 100,000 elements, the reported time
• Loop1: 4.212 sec
• Loop2: 0.016 sec
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 42