You are on page 1of 2

05a - Using Collections

1 General introduction
The next page describes four exercises, all aimed at implementing specialized
forms of collections. A few general remarks:

All these exercises rely on using delegation far more than on other
behaviour-extending mechanisms. If you do not understand delegation,
check for example http://en.wikipedia.org/wiki/Delegation_pattern

Take a structured approach:


o

Make a high-level design first. What classes do you need? What


interfaces to implement? What tasks to delegate?

Implement the base class and its interfaces.

Think on how to test the result you'll have at the end. Even better is
to define test code before starting the rest.

Make designs for the relevant methods. An IPO and a NassiSchneidermann diagram are good things to make when doing this.

Write the code.

Test the code.

2 Exercises
2.1 Observable List
We need a List which also is Observable. That means:

It is a concrete class, not an interface itself

It implements the interface List

It is also an Observable, so it extends that class

Your task is to implement this. Do not use the JavaFX class ObservableList; the
idea of this exercise is not to just take a canned class.

2.2 Custom Queue


A queue stores the objects it gets fed in some internal mechanism. We do not
know what mechanism it uses, and in general, we don't care. But sometimes we
need more control. One example would be that we need a queue which would
discard double objects, in effect using a Set as its internal storage mechanism.
Write a class implementing the Queue interface. This class has a constructor with
a Collection as a parameter; this Collection would be the internal storage of the
Queue.

2.3 Blocking Queue


Extend the class from the previous exercise to create a BlockingQueue. Make
sure to implement the interface BlockingQueue<E> (check the Java API!)

2.4 SortedHashMap
We need a special form of a Map:

This Map can be parameterized as any Map: Map<K, V>

It behaves as a Map, but also as a Sorted Set


o

So it implements the interface Iterable

Sorting order is based on the natural ordening of the Values, not the Keys
o

So it implements the interface Iterable<V>

You might also like