//********************************************************************// //*************Collection Algorithms - October 31st, 2016************// //******************************************************************// -Extra office hours due to an emergency concert attendance for Simpkins on Wednesday ----------------------------------------------------------- -Collection Algorithms are static methods in the "Collections" class that can sort, find the min/max element, search, etc. -e.g. "public static <T extends Comparable<? super T>> void sort(List<T> list)" -shows us how to declare a generic method by putting a type parameter before the return type -This particular sort uses the "natural ordering" - aka, via that classes implementation of Comparable -<? super T> is a TYPE BOUND that means "a superclass of T", meaning that the whole <T extends ... >> means that either T, or a parent class of T, must implement the "Comparable" class -the "?" is a wildcard character that allows subclasses to be passed in as well, rather than having to implement Comparable directly -Type bounding gets waaaayyyyyyy more involved, but this is what you need to know for now -DESTRUCTIVE METHOD - alters the data that is passed into it (like a side effect?) -Let's say that we have a class (e.g. "Trooper") that we'd like to extend (e.g. "StormTrooper") -We'd like to implement the comparable interface, so that we can use the sort method -The Comparable interface (dun-dun-DUN): "public interface Comparable<T>{ public int compareTo(T o); }" -SIDE NOTE: It's not "Alphabetic sorting", it's "lexicographical" ordering -"This'll do wonders for your dating life, trust me"-Simpkins 2016 -CompareTo(): -returns a + number if the object is greater than the passed in element -returns 0 if they are equal -returns a - number if it is less than the passed-in object -NOTE: Objects that are "equal" in "Comparable" ARE NOT NECESSARILY ".equals()" EQUAL!! -example implementation: "int compareTo(Magazine o) { return this.issueNumber - o.getIssueNumber(); }" -A side-note: Java uses something called TYPE ERASURE for their Generics implementation -<NOTES ON SLIDE> -To let Java Generics work with non-generic methods (e.g. using the "Magazine" type above instead of <T>), Java will write BRIDGE METHODS behind the scenes -Now, what if we want to have MORE THAN 1 criteria? E.g. A magazine that is ranked by its quality AFTER the issue #? -We can use the COMPARATOR interface! -The Comparator<T> interface can either add extra ordering to a class that already uses Comparable, or it can add sorting functionality for classes that haven't implemented compare() -has two methods: "int compare(T o1, T o2)" and "boolean equals(Object o)" -By default, equals inherits from "Object", so you only really need to implement compare() -LOOK AT MORE EXAMPLES TO CONFIRM I KNOW EXACTLY HOW THIS WORKS -The power of this is that we don't have to rewrite the sort method for every single class; we can just use Java's ONE provided sort method, and we can sort almost any kind of object with one chunk of code -It's written using the "Comparable" interface type behind the scenes- this is VERY powerful when used properly -Programming exercise in the slides for "Collection algorithms"; we'll come back to this later