Core Java

Collections

Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.

.jpg

Copyright © Cay S. Horstmann 2016

Except where otherwise noted, this work is licensed under .svg

Understand the benefit of separate collection classes and interfaces

Collection Interfaces

Collection Classes

Become familiar with the types in the collections framework

Fundamental Interfaces

Using Iterators

More about Iterators

.gif

Useful Collection Methods

int size()
boolean isEmpty()
boolean contains(Object obj)
boolean containsAll(Collection<?> c)
boolean equals(Object other)
boolean addAll(Collection<? extends E> from)
boolean remove(Object obj)
boolean removeAll(Collection<?> c)
boolean removeIf(Predicate<? super E> filter)
void clear()
boolean retainAll(Collection<?> c)
Object[] toArray()
<T> T[] toArray(T[] arrayToFill)

Interfaces in the Collection Framework

Lists

Concrete Collections

ArrayList An indexed sequence that grows and shrinks dynamically
LinkedList An ordered sequence that allows efficient insertion and removal at any location
ArrayDeque A double-ended queue that is implemented as a circular array
HashSet An unordered collection that rejects duplicates
TreeSet A sorted set
EnumSet A set of enumerated type values
LinkedHashSet A set that remembers the order in which elements were inserted
PriorityQueue A collection that allows efficient removal of the smallest element
HashMap A data structure that stores key/value associations
TreeMap A map in which the keys are sorted
EnumMap A map in which the keys belong to an enumerated type
LinkedHashMap A map that remembers the order in which entries were added
WeakHashMap A map with values that can be reclaimed by the garbage collector if they are not used elsewhere
IdentityHashMap A map with keys that are compared by ==, not equals

Collection Classes

.png

Work with linked lists and array lists

Why Linked Lists?

Linked Lists

Concurrent Modifications

linkedList

Other Lists

Java 9 News Flash - Collection Literals

.png

Map Literals

Work with hash sets and sorted sets

Hash Sets

set

Tree Sets

treeSet

Work with queues, deques, and priority queues

Queues, Deques, and Priority Queues

priorityQueue

Use maps to organize key/value pairs

Maps

map

Updating Map Entries

Map Views

Linked Hash Maps

Understand collection wrappers and views

Views

Restricted Views

Use common algorithms with collections

Common Algorithms

Bulk Operations

Converting between Collections and Arrays

shuffle

Be able to use collections from old versions of Java

Legacy Collections

.gif

sieve