Collections
Collections
Java 11 (1Z0-819)
SortedSet Deque
access at
both ends
NavigableSet
ArrayList LinkedList ArrayDeque PriorityQueue
Stack
NavigableMap
LinkedHashMap insertion-order
Object
TreeMap sorted
Arrays Collections
implements
extends
Copyright © Seán Kennedy
Index: 0 1 2 3 Duplicates allowed.
List: Value: “Dublin” “Paris” “London” “Paris”
Head Tail
Set:
“Dublin” “London”
FIFO
Queue: “Red” “Green” “Blue”
“Blue” Head/Tail
Hashcodes (buckets): 39 19
LIFO “Green”
Map: Queue
Values: “Amy”, “May” “Bob” (Stack):
“Red”
Copyright © Seán Kennedy
Popular Collection Methods
boolean add(E element) adds the element to the end
boolean remove(Object o) removes a single instance of the element specified
boolean removeIf(Predicate<? super E> p) removes all elements that match the condition
void forEach(Consumer<? super T> c) performs the given action on all elements in the
Note: this method is a default method collection
in the Iterable interface and Collection
extends Iterable.
Code: CommonCollectionMethods.java
Copyright © Seán Kennedy
CommonCollectionMethods.java
Code: UsingLists.java
Copyright © Seán Kennedy
Popular List Methods
void add(int index, E element) adds the element at the index and moves the rest
down one place
E get(int index) returns the element at that index
E remove(int index) removes the element at that index and moves the
rest up one place
void replaceAll(UnaryOperator<E> op) replaces each element in the list by applying the
operator
E set(int index, E e) replaces the element at that index with the
specified element (the original is returned)
Code: UsingLists.java
Copyright © Seán Kennedy
UsingLists.java
13
14
15
Code: UsingSets.java
Copyright © Seán Kennedy
UsingSets.java
void clear() removes all keys and values from the map
boolean containsKey(Object key) is the key in the map
boolean containsValue(Object value) is this value in the map
Set<Map.Entry<K,V>> entrySet() returns a Set view of the key/value pairs
void forEach(BiConsumer(key, value) perform the given BiConsumer on each
entry in the map
V get(Object key) returns the value for the specified key or
null if no mapping exists
boolean isEmpty() is the map empty
Code: UsingMaps.java
Copyright © Seán Kennedy
Popular Map Methods
Set<K> keySet() returns a Set view of all the keys in the map
V put(K key, V value) adds or replaces the key/value pair. Returns previous
value or null.
V putIfAbsent(K key, V value) adds key/value pair if key not there already and
returns null; otherwise, returns existing value.
V remove(Object key) removes the entry if the key exists and returns the
value that was there; returns null if key not in map.
V replace(K key, V value) replaces the value for the key and returns the old
value; returns null if key not in map
void replaceAll(BiFunction<K,V,V> fn) replaces each value with the results of the function.
int size() how many key/value pairs in the map
Collection<V> values() returns a Collection view of all the values
Code: UsingMaps.java
Copyright © Seán Kennedy
UsingMaps.java
• LinkedList
• as LinkedList implements Queue; basic queues can be
handled with a LinkedList
23
• PriorityQueue
• PriorityQueue orders the elements relative to each other
such that “priority-in, priority-out” (as opposed to a FIFO
or LIFO).
• the elements are either ordered by natural order or by a
custom order via a comparator.
• elements that are sorted first will be accessed first.
24
• Deque
• deque (“double ended queue”) and is pronounced “deck”.
• access from both ends permitted.
• can be used as both FIFO (queue) and LIFO (stack).
• ArrayDeque
• expandable-array implementation of the Deque interface
(no capacity restrictions).
• API: “likely to be faster than Stack when used as a stack,
and faster than LinkedList when used as a queue”.
25
The most common methods are peek(), offer() and poll() as they do not throw exceptions.
POP is useful for remembering them.
Code: UsingQueues.java
Copyright © Seán Kennedy
Popular Deque Methods
Code: UsingQueues.java
Copyright © Seán Kennedy
Using Deque as a queue