7. Sets versus Ordered Sets as the Values of Reference Properties

Recall that a set is an unordered collection that has unique elements, while a list is an ordered collection that may contain the same element more than once.

When a non-functional association end is ordered, like Book::authors in the following class diagram, this implies that the range of the corresponding reference property needs to be an ordered collection type, such as the Java collection interface types SortedSet and List.

Figure 5.1. A set-valued reference property authoredBooks and an ordered-set-valued reference property authors
A set-valued reference property authoredBooks and an ordered-set-valued reference property authors

In theory, set-valued reference properties should be defined with Set<T>, such as declaring Set<Book> as the range of Author::authoredBooks, while ordered-set-valued reference properties should be defined with SortedSet<T>, such as declaring SortedSet<Author> as the range of Book::authors. The Java interface type Set is typically implemented by the Java class HashSet, while SortedSet is typically implemented by TreeSet.

However, if there is no concern about avoiding duplicate elements, both set-valued and ordered-set-valued reference properties can also be defined with the generic interface type List<T> as their range, which is typically implemented with ArrayList.

When the main use cases are adding elements to the collection and later iterate over them, the best choice is using List implemented by ArrayList, which is more memory efficient than LinkedList or any implementation of Set, and has fast insertion, iteration, and random access. Using HashSet provides a performance benefit only in the use case of finding an element in the collection.