Labs
In these labs we will use the interfaces and sorting methods from this chapter to sort data.
Starter code: CollectionSorting/com.example.sorting.labs.
Solutions: com.example.sorting.solutions.labs
- The
PlanetSorterclass loads and prints a list ofPlanetobjects. Create aComparatorthat will sort planets in reverse order of orbit. UseCollections.sortand theComparatorto sort the list before printing it.
(Solution: PlanetReverseOrbitComparator.java, PlanetSorter.java)
- Change
PlanetSorterto have the list sort itself instead of usingCollections.sort.
(Solution: PlanetSorter2.java)
- The access_log file contains records of hits to a web site. Consider each line equivalent to one hit, and the first space-separated word on each line identifies the visitor. Write a Java application that reads this file a line at a time, and counts the number of hits from each unique visitor, storing the results in a
Map.
When all the records have been read, display the unique visitors along with the corresponding number of total hits. (Hint: Use String's split() method to get the first field of each line.)
(Solution: ShowHits.java)
- (Optional) Modify your solution to sort the displayed records according to the total number of hits for each visitor.
(Solution: ShowSortedHits.java)