Tracking song popularity on your jukeboxHere’s what you have so far, without the sort:But the ArrayList class does NOT have a sort() method!ArrayList is not the only collectionYou could use a TreeSet... Or you could use the Collections.sort() methodAdding Collections.sort() to the Jukebox codeBut now you need Song objects, not just simple StringsChanging the Jukebox code to use Songs instead of StringsIt won’t compile!The sort() method declarationGenerics means more type-safetyLearning genericsUsing generic CLASSESUsing type parameters with ArrayListUsing generic METHODSHere’s where it gets weird...Revisiting the sort( ) methodIn generics, “extends” means “extends or implements”Finally we know what’s wrong...The Song class needs to implement ComparableThe new, improved, comparable Song classWe can sort the list, but...Using a custom ComparatorUpdating the Jukebox to use a ComparatorUh-oh. The sorting all works, but now we have duplicates...We need a Set instead of a ListThe Collection API (part of it)Using a HashSet instead of ArrayListWhat makes two objects equal?How a HashSet checks for duplicates: hashCode() and equals()The Song class with overridden hashCode() and equals()And if we want the set to stay sorted, we’ve got TreeSetWhat you MUST know about TreeSet...TreeSet elements MUST be comparableWe’ve seen Lists and Sets, now we’ll use a MapFinally, back to genericsUsing polymorphic arguments and genericsBut will it work with ArrayList<Dog> ?What could happen if it were allowed...Wildcards to the rescueAlternate syntax for doing the same thing