CS2514

Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Java's Collections Framework

Java's Collection interface

The Java interface java.util.Collection includes the following:

add(o) Add object o to the collection
contains(o) Returns true if this collection contains an element that equals o
isEmpty() Returns true if this collection contains no elements
iterator Returns an iterator over the elements in this collection
remove(o) Removes an element that equals o from this collection if such an element exists
size() Returns the number of elements in this collection

Java's Set interface

Java's List interface

Java's Map interface

Which Java interface would you choose?

(More than one answer may be plausible in some cases)

Implementations

Once you've decided which Java interface to use, you choose an implementation (or implement a new one of your own)

InterfaceImplementations
Set HashSet
& its subclasses
A hash table
List ArrayList Resizable array
LinkedList Doubly linked list
Map HashMap
& its subclasses
Hash table
SortedSet TreeSet Balanced binary tree
SortedMap TreeMap Balanced binary tree

(Where Java needs to test for equalkity of objects in these data structures, it wil use your equals method. For the hash tables, it will use your hashCode method)

Traversing collections

Traversing collections

Iterators

A new for-loop