Collections - Compare


Collections - Compare

HCCClass.java
ClassCreditComparator.java
ArrayListExample.java

read ch 16.1-16.7, 20


Classwork


Part A

Create a class Book which has instance variables for author, title, publisher, pages, and price. 

In a separate class, in a main, create a List of Books, point it at a LinkedList, and add at least 10 books to the list. Make sure you have  more than one book with the same title but different authors, same author but different title, and similarly some with same publisher, same pages, and same price  ("Book 1" by "Author 1" and similar are fine).  This will be easier if Book has a friendly parameterized constructor.

Print the list and make sure your toString  results in a readable list

Make Book Comparable so that Books are sorted alphabetically by author  (hint: String is Comparable) then by title. (that is,  if the authors are the same, use the title)

In your main, sort the Books and print the sorted list.


Part B

Create a class that implements Comparator for Book.  In this case, sort  by pages, then by price.

In your main, sort the Books using the Comparator and print the results.


[EC] Create another Comparator that lets you sort by publisher, then author, then title.  [hint: you've already got something that can handle the last part.]  In main, sort by this and print the results

[EC] Add a method cheapen in the main class which takes a List of Books and removes from it any that cost more than 20 cents per page.