HashSet, TreeSet, Comparable and Comparator ------------------------------------------- This is an excursion into implementing sets of objects using java parameterised classes HashSet and TreeSet. We perform experiments using a class Person, having first and econd names, and a class Species, having a name. Person ------ Has a first and second name. It implements the Comparable interface and therefore must implement the compareTo method. This then allows persons to be compared p1.compareTo(p2) Species ------- Has a name, and that is all. It also implements the Comparable interface. Car --- Has a make and a model. It does NOT implement Comparable. However we define a class CarComparator, and this can be created and passed to the TreeSet constructor Test0 ----- We produce a TreeSet and add persons into ths set. The duplicate "Prosser, Patrick" is not entered as the TreeSet uses the compareTo method within class Person Test1 ----- We produce a TreeSet and add species to the set. As in Test0 duplicates are not added to the set (species "cat"). This just showsthat there is nothing peculiar about person and Test0 Test2 ----- We produce a TreeSet and when constructing it pass a CarComparator. Again, the TreeSet works as advertised with the duplicate "Ford Mustang" appearing once only. Test3 ----- This uses HashSet and this does not work as advertised. We cannot pass a Comparator during construction of the HashSet. Maybe this is so to allow efficient hashing