LinkedList Flashcards
FIFO
first in, first out - a queue: first in line gets into theater first; a call center, etc
LIFO
last in, first out - like a stack of plates
removing head/first element from LinkedList vs ArrayList
LinkedList: O(1)
ArrayList: O(n)
if the list needs to be sorted…
…use an ArrayList - sorting accesses items by index
examples of LinkedList
list of orders to fultill FIFO list of customers to contact list of successful and failed calls (??) list of books to download list of categories (almost always printed together) (??)
examples of ArrayList
list of items in an order - customer may want to access or edit any item
list of names to sort - sorting accesses items by index
instructions - made need to interrupt and start over
list of lines in a file - editing often accesses lines by index
Best when manipulating the ends of a list
the one thing array is better suited for
get(index) - O(1) for array; O(n) for linkedlist
Deque interface
==”double ended” queue
pronounced “deck”
LinkedList implements methods from the Deque interface
end-related methods
addFirst()
addLast()
removeFirst()
removeLast()
linkedlist descendingIterator()
LinkedList list = new LinkedList<>(); list.add("A"); list.add("B"); list.add("C"); Iterator x = list.descendingIterator();
while(x.hasNext()) {
System.out.println(“value is: “ + x.next());
}
which data structures have a backing array
ArrayList, HashMap, HashSet
initialCapacity is 16
which data structure for DNS is best?
HashMap since we need to get any element in O(1) time.
HashSet cannot retrieve a single item, can only say whether the structure contains the item.
can HashSet have a key-value pair?
no, HashSet takes in only 1 generic: HashSet where E is an object
in a HashMap hashExample, what happens:
hashExample.add(blue, 3);
hashExample.add(blue, 5);
The key-value pair “blue, 5” overwrites “blue, 3”. If both values should be saved, the structure should be HashMap.
–>note that HashMap is a kind of Set, where the key cannot hold duplicate values. In HashSet however, we can only hold “keys” not a meaning or value associated with the key.
does HashMap preserve insertion order?
no, HashMap is a collection of Key and Value but HashMap does not give guaranty that the insertion order will preserve
note: LinkedHashMap retains insertion order
TreeMap can sort the keys