Defining and Iterating Collections
Richard Warburton
@richardwarburto | www.insightfullogic.com
Collection of Interface vs
Collection Behaviors
Collections Implementation
The Collection of Collections
Collection
List Set Queue Map
SortedSet Deque SortedMap
Interfaces Implementation
●
Multiple Data Structures ●
Specific Data Structure
●
Functional Characteristics ●
Performance Characteristics
●
Prefer as variable type ●
Concrete and Instantiable
●
Often has a popular
implementation
Collection
List Map
Set Queue
ArrayList HashMap
HashSet PriorityQueue
LinkedList
Deque
SortedSet SortedMap
LinkedList
TreeSet TreeMap
ArrayDeque
Set Elements are Keyed? Map
Yes
No
No
No
Order is Important? Order is Important?
Yes Yes
Yes
Elements are Unique?
SortedSet No SortedMap
No
Last In, First Out? First In, First Out?
No
Yes Yes
Yes
List Deque Queue
Collection Behaviors
Iteration, Sizing and Mutation
Iterable
Iterator
Collection
Outline of Collection Interface
size() Get the number of elements in the Collection
isEmpty() True if size() == 0, false otherwise
add(element) Add the element at the beginning of this collection
addAll(collection) Add all the elements of the argument collection to this collection
remove(element) Remove the element from this collection
removeAll(collection) Remove all the elements of the argument collection to this collection
retainAll(collection) Remove all the elements of this collection not in the argument collection
contains(element) True if the element is in this collection, false otherwise
containsAll(collection) True if all the elements of the argument collection are in this collection
clear() Remove all elements from this collection
Summary
The Collection of Collections
When to pick a certain collection
Common Collection Features