You are on page 1of 18

Java Fundamentals: Collections

WHAT ARE COLLECTIONS AND WHY USE THEM?

Richard Warburton
JAVA CHAMPION, AUTHOR AND PROGRAMMER

@richardwarburto www.monotonic.co.uk
Project & Array Problem
Introduction & Course Outline
Implementing Data
Structures Is Hard!
Data Structures Are Diverse

Ordering

Pairs

Uniqueness
Course Outline

Introduction to
Java Streams
Collections

Lists: Collections with Collection Operations &


Iteration Order Factories

Sets: Collection with


Maps: Collections of Pairs
Uniqueness
Collection of Collections
Collection

Map

List Set Queue

SortedMap
SortedSet Deque
Collection Design

Interfaces Implementation

Multiple data structures Specific data structures

Functional characteristics Performance characteristics

Prefer as variable type Concrete and instantiable

Often has a popular implementation


Collection

Map
HashMap
List Set Queue
ArrayList
HashSet PriorityQueue
LinkedList

SortedMap
TreeMap
SortedSet Deque
LinkedList
TreeSet
ArrayDeque
Elements
Set are keyed? Map

Order is Order is
important? Elements important?
must be
unique?

SortedSet SortedMap
Insertion
order
List matters? Deque
Collection Behaviors
Iterable

Iterator

Collection
size() t Get the number of elements in the
collection

isEmpty() t True if size() == 0, false otherwise

t Ensure that an element is in the collection


add()

t Add all the elements of the argument


addAll() collection to this collection
t Remove the element from this collection
remove(element)

t Remove all the elements of the argument


collection to this collection
removeAll(collection)

t Remove all the elements of this collection


retainAll(collection) not in the argument collection
t True if the element is in this collection,
contains(element) false otherwise

t True if all the elements of the argument


containsAll(collection) collection are in this collection

clear()
t Remove all elements from this collection
Java Collections are useful and versatile
Summary
There’s a collection of collections

When to use specific collections

Common collection features

You might also like