You are on page 1of 15

Java Course

Module 8 Core Java API: Collections

Module Objectives
At the end of this module, participants will be
able to:
Identify the core Collections interfaces and
their behavior.
Use implementations of the various
Collections interfaces.

Overview of the Java Collections API


The Java Collections API is a set of interfaces and
implementations included in the Java standard library.
A Collection or a Container is an object that groups together
multiple elements into a single unit.
Each collection implements different storage, retrieval and
manipulate behaviors.
Found under the java.util package.

Overview of the Java Collections API (cont.)

The API contains Interfaces that specifies the abstract


behavior of each collection.
The API contains concrete implementations of the interfaces.
Each concrete implementation uses different algorithms and
behavior.

Overview of the Java Collections API (cont.)

Collection

Set

List

Map

Queue

Sorted Map

Sorted Set

Collection Interface
Root of the collections hierarchy.
The Collections interface represents general purpose behavior
for the interfaces in the hierarchy.
Does not have a concrete implementation.
Used in order to obtain a general reference to any kind of
Collection.
** Refer to the CollectionsSample.java sample code

Set Interface

A Set is a Collection that does not allow duplicate entries.


A Set Has the same methods as the Collection interface.
Can contain at least 1 null element.
Commonly used implementations are HashSet, LinkedHashSet
and TreeSet.
Elements in a HashSet and LinkedHashSet are not sorted.
Elements in a Treeset are sorted.
** Refer to the SetSample.java sample code

List Interface
A List is an ordered collection of elements.
May contain duplicates.
An elements indexing is similar to arrays, with the first element
in index 0.
Contains methods that allow manipulation of elements based on
its position in the sequence.
Common implementations are ArrayList and LinkedList.
** Refer to the ListSample.java sample code

Queue Interface
A Queue is a collection for holding elements typically in a FirstIn-First-Out order.
Has versions for a set of methods inherited from the Collections
interface that return special values instead returning an
exception when failing.
Throws Exception

Returns special value

Insert

add()

offer()

Remove

remove()

poll()

Examine

element()

peek()

** Refer to the QueueSample.java sample code


9

Map Interface
A Map is a collection that pairs a key to an element contained in
the collection.
The key and the element can be any Object.
A key is assigned to an element when it is added to the map
using the put(<key>, <element>).
The key is used to retrieve an element from the Map using the
get(<key>) method.
** Refer to the MapSample.java sample code

10

Ordering
A collection can be sorted using the Collections.sort() method.
Elements in a collection implement the Comparable interface
which defines the sorting order.
Elements that do not implement the Comparable interface
cannot be sorted and will throw an exception.

11

Comparable Interface
The Comparable Interface can be implemented by any class
that wants to define its natural ordering.
Classes that implement the Comparable interface will need to
define the compareTo(Object) method which defines how an
instance of the class orders against the specified object.
The compareTo(Object) method should return:
0 if the instance and the specified object are equal.
A negative integer if the instance is less than the specified object.
A positive integer if the instance is greater than the specified object.

12

Comparator Interface
The Comparator interface is similar to the Comparable interface
except that it accepts two objects to be compared as parameters
The Comparator interface can be implemented to compare two
objects that are not Comparable
Classes implementing the Comparator interface will need to
override the compare(Object A, Object B) method to return the
following:
0 if A and B are equal.
A negative number if A is less than B.
A positive number if A is greater than B.
** Refer to the Car.java and ComparableSample.java sample code

13

Questions and Comments

14

Activity
1)
2)

3)

4)

5)
6)

7)
8)

Open your SEF workspace in Eclipse.


In the package explorer, navigate to the
following package sef.module9.activity.
Review the interfaces Radar and
RadarContact and note the requirements
of the interfaces.
Complete the implementation of
RadarContactImpl based on the
RadarContact interface.
Test the implementation using the
RadarContactTest JUnit driver.
Complete the implementation of the
DistanceComparator class.
Complete the implementation of
RadarImpl based on the Radar interface.
Test the implementation using RadarTest
JUnit driver.
15

You might also like