Document 11586321

advertisement
1.00 Tutorial 12
Collections, Sorting
1
Agenda
• Final Exam
• Quick review:
– Collections framework
– Sorting
• Exercises: using Collections.sort and
Comparator to sort on multiple keys
2
Final Exam
•
•
•
•
Time: 3 hours
Place: DuPont (map*)
Review Session: To Be Determined
Sample Exams: will be on website
MIT WebSite
3
Review: Java® Collections
Map
• Review Lecture 32 notes on your own
Collection
Set
SortedSet
SortedMap
List
Iterator
ListIterator
Exercise: briefly explain what each interface represents.
Give examples of concrete classes if you can.
Answer: Collection is an “unordered multiset” (may contain duplicates) that serves
as the most general type of collection. Set is also unordered, but does not allow
duplicate objects. SortedSet is like Set, but imposes an order. In other words, the
Iterator returned by SortedSet.iterator() will return the elements of the set in order
from lowest to highest. List is ordered (a “sequence”), and has the listIterator
method to return a ListIterator. List allows elements to be retrieved using an index
via the get(int index) method. The Iterator interface is very simple; it only has 3
methods: hasNext(), next() and remove(). ListIterator adds bi-directional support
via previous() and similar methods, and adds the set() method. Map is the basic
interface for data structures that map keys to values. SortedMap imposes an order
on the keys (but not on values). An example of a SortedMap was our binary search
tree.
4
Review: Sorting
• Many sorting algorithms exist; we looked at just a
couple:
– Insertion sort: O(n2)
– QuickSort: O(n log n)
• Also very common are
– Bubble sort: O(n2)
– Merge Sort: O(n log n) [used in JDK]
• Very cool visualization of sorting algorithms at
http://cg.scs.carleton.ca/~morin/misc/sortalg/
5
Sorting the Easy Way: via JDK!
• You can sort easily without writing your
own sort routine. Use Collections.sort()
– For lists of objects that have a natural order
(like Integer, Double, String, etc.) use:
sort(List list);
– For lists of other objects use:
sort(List list, Comparator c);
6
Simple Example: Sorting Strings
ArrayList strings = new
strings.add("Peilei");
strings.add("Bharath");
strings.add("Jud");
ArrayList();
strings.add("Curtis");
strings.add("Elana");
strings.add("Steve");
ListIterator li = strings.listIterator();
System.out.println("Before sorting: ");
while (li.hasNext())
System.out.println(li.next());
Collections.sort(strings); /* EASY! */
System.out.println("After sorting: ");
li = strings.listIterator();
while (li.hasNext())
System.out.println(li.next());
Full source code: StringSort.java
7
Complex Example: Multiple Keys
• Use a Comparator class to perform custom
sorting based on multiple sort keys
• Recall how a Comparator’s compareTo(a,b)
method works:
returns
when
a<b
a negative integer
a=b
0
a>b
a positive integer
8
But first, a sample Comparator
• Exercise: write a Comparator class for
Integer objects using intValue()
public int compareTo(Object o1,
Object o2) {
Integer i1 = (Integer)o1);
Integer i2 = (Integer)o2;
return ???;
}
Answer: i1.intValue() – i2.intValue();
9
Multiple Sort Key Example
• Classes:
– TA: represents your friendly 1.00 TA
– Section: represents a 1.00 section
– Student: represents you
– StudentComparator: compares Student objects
– Tut12Main: main method, makes Student objects
for all students in 1.00 this term and sorts them in
interesting ways
10
Multiple Sort Key Example
• What is a sort key?
– For a student, we may want to sort on last
name, first name, tutorial section, TA, email
address
– We might even want to sort on more than one
of these! E.g., on TA 1st, and then on tutorial
section 2nd (when TAs are the same), then on
first name 3rd (when both TAs & sections are
the same), etc.
11
Multiple Sort Key Example
• Let’s go through the code in order:
– TA.java
– Section.java
– Student.java
– Tut12Main.java
– StudentComparator.java
• Run it!
When running the code, be sure to point out why it’s useful to sort by multiple keys.
For the 2nd example, where we use “first” and “last” name as keys, point out that
there are 3 “Amanda”s and 3 “Jessicas”. For the last example (using four keys), you
can show the two “Amanda” that are in Bharaht’s section: we actually have to use
all four sort keys in order to give them an order!
Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and
other countries.
12
Download