Ask the teacher for a discussion about the solution upon the completion of each single exercise, unless explicitly differently said in the exercise text.
Observe it.unibo.oop.lab05.ex1.UseCollection
, and use it as example to complete the exercise in it.unibo.oop.lab05.ex1.UseSet
.
Before proceeding, please read and make sure you understand the following notes.
Natural Ordering: Elements in a TreeSet
are sorted by natural order, i.e., the type X
used within TreeSet<X>
must implement the interface Comparable<? super X>
. As such, for instance:
-
A
TreeSet<String>
allows new elements to be added by using theadd
method becauseString
implementsComparable<String>
; -
conversely, trying to add elements of type
MyType
to aTreeSet<MyType>
, in the hypothesis thatMyType
does not implementComparable<MyType>
, would raise a run-time error of typeClassCastException
(details on exceptions and error handling will be provided in the next lessons).
Concurrent modifications: All the iterators created from instances of the TreeSet<E>
class (actually, iterators created by any of the collections provided with the JDK standard library) are fail-fast.
If the collection instance on which the iterator is operating is modified while an iteration is in progress, the iterator will produce a run-time error (a ConcurrentModificationException
, to be precise).
The reason is that the iterator may end up in an inconsistent state: it is iterating over a collection, but the collection has changed! What is the next element? And, in case of an unordered collection (such as an HashSet
), how to make sure we are not visiting the same element again?
Since those error may generate inconsistencies which lead to bugs very hard to spot and reproduce, Java takes the conservative stance of halting when such a behavior is detected.
Note that more "permissive" languages (such as Javascript) don't enforce the same policy, and leave to the implementor the burden of dealing with inconsistent iteration states.
- try to write a
for
-each cycle iterating overTreeSet
(which, internally, generates and uses anIterator
), and within the cycle try to remove an nelement from theTreeSet
. What happens?
The correct way to remove elements from a collections while iterating it is to use the remove()
method of Iterator
.
This requires a reference to the iterator, and, as such, cannot be used from within a for
-each cycle.
-
Follow the comments in
it.unibo.oop.lab05.ex2.UseSetWithOrder
, and create a program that sorts aTreeSet<String>
using a customComparator<String>
(to be created separately, from scratch). -
Refer to the java documentation to understand how to create a
Comparator
(interface documentation).
-
Implement
WharehouseImpl.java
andProductImpl.java
by following the contracts described in their relative interfaces. -
Refer to the code documentation for details about the implementation
-
Run
UseWarehouse
to test the program
-
This exercise is an extension of the previous: reuse as much as possible of the previously produced (working) code.
-
In order to run tests, complete the
main
method ofUseWarehouse.java
initializing correctly the variables of typeProduct
andWarehouse
.
-
Implement the functions in the class
Utilities.java
. -
Verify their behavior by using
UseUtilities
.
For better understanding of the symmetric difference operation between two sets refer to the following diagram (in red the expected result):