Why data structures
Queues
Stacks
Lists
Maps
Summary
Computer programs need
to store data
e.g. keep array of user names
The right data structure might make a program run 10x or more faster
e.g. search for contact in your phone, get or add new item in your shopping list
Some examples
Do you have more?
Front
Back
Add
Poll
First In First Out (FIFO)
Ivancho is in EVN office to pay his bill
and... disaster strikes!
They need a hero (or a good programmer) to fix that!
In a galaxy far far away....
ArrayDeque<Integer> queue = new ArrayDeque<>();queue.add(1);why not int?
Add element at the back of the queue
Create empty queue of integers
Get element from the head of the queue and REMOVE IT
int element = queue.poll();int element = queue.peek();Get element from the head of the queue and DO NOT remove it
Check if the queue is empty
int elementCount = queue.size();Check how many elements are in the queue
boolean isQueueEmpty = queue.isEmpty();Some examples
Do you have more?
Top
Bottom
Push
Pop
Last In First Out (LIFO)
Ivancho is again in the EVN office to pay his bill
Can Ivancho skip the line?
Stack<Integer> stack = new Stack<>();stack.push(1);Add element at the top of the stack
Create empty stack of integers
Get element from the top of the stack and REMOVE IT
int element = stack.pop();int element = stack.peek();Get element from the top of the stack and DO NOT remove it
Check if the stack is empty
int elementCount = stack.size();Check how many elements are in the stack
boolean isStackEmpty = stack.empty();How to decide what size your array should be?
Ivancho have to buy all the items from the near
grocery shop that his mom ordered him.
Ivancho has lousy memory but still can do it!
Idea: create a shopping list and store all items in it.
He has no pen and paper. Can he write an app?
ArrayList<String> list = new ArrayList<>();list.add("Semki");Add element of the back of the list
Create empty list of strings
list.get(0);list.remove(0);Remove an item at specified index
Get an item at specified index
list.set(0, "Slaninka");int elementCount = list.size();Get the size of the list
Add item at specific place (index)
Some examples
Do you have more?
His mom asks him to visit the grocery shop again!
This time the task is just a bit (maybe more) harder.
He has to buy some groceries in different quantities.
His reward will be a week in Amsterdam,
close to the Red Light District...
Ivancho is very happy with his experience
with Mariika in Galaxy (hmm?).
Ivancho is very
enthusiastic about it!
He just have to modify his app.
Can you help?
HashMap<String, Integer> map = new HashMap<>();map.put("Krushi", 3);Add element with a key
Create empty map of strings to integers
int quantity = map.get("Krushi");map.remove("Krushi");Remove an item with key
Get value for a key
boolean hashKrushi = map.contains("Krushi");int elementCount = map.size();Get the size of the map
Check if contains value with key