Tracking song popularity on your jukeboxYour first job, sort the songs in alphabetical orderGreat question! You spotted the diamond operatorExploring the java.util API, List and CollectionsIn the “Real-World”™ there are lots of ways to sort“Natural Ordering,” what Java means by alphabeticalBut 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 ComparatorFill-in-the-blanksBut wait! We’re sorting in two different ways!Sorting using only ComparatorsJust the code that mattersWhat do we REALLY need in order to sort?Enter lambdas! Leveraging what the compiler can inferWhere did all that code go?Some interfaces have only ONE method to implementUpdating the Jukebox code with lambdasReverse EngineerUh-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()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 MapCreating and filling collectionsConvenience Factory Methods for CollectionsFinally, back to genericsUsing polymorphic arguments and genericsBut will it work with List<Dog>?What could happen if it were allowed...?We can do this with wildcardsUsing the method’s generic type parameterExerciseBE the Compiler, advancedExercise SolutionFill-in-the-blanks“Reverse Engineer” lambdas exerciseSorting with lambdasTreeSet exerciseBE the Compiler solution